Thread: const question

  1. #1
    Fish
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    6

    const question

    Hi all

    I'm a little confused about the difference between:

    bool const Foo();

    and

    bool Foo() const;

    I know that the latter promises not to change the object, what does the former promise?

    Thanks in advance

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> what does the former promise?
    Not much. It's the same as "const bool Foo()" - which means that Foo() returns a bool which is constant.

    Returning a constant built-in type (or POD type) should be avoided.
    http://www.gotw.ca/gotw/006.htm

    gg

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    22

    Just be careful with pointers

    Quote Originally Posted by Codeplug
    >> what does the former promise?
    Not much. It's the same as "const bool Foo()" - which means that Foo() returns a bool which is constant.

    Returning a constant built-in type (or POD type) should be avoided.
    http://www.gotw.ca/gotw/006.htm

    gg
    I know you're not talking pointers in your example but just to make sure you realize this, const char *Foo() is not the same as char * const Foo().

    The first is a normal pointer to a constant char string (pointer can be changed but not the string being pointed to).
    The second is a constant pointer to a normal char string (string being pointed to can be changed but not the pointer).

    A subtle difference.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You should note that this:

    bool Foo() const;

    implies that Foo() is a method in a class. A const function that is not part of a class has no meaning because an object doesn't call the function.

  5. #5
    Fish
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    6
    7stud:

    I knew my audience would know I spoke of a member function.

    Codeplug confirmed my suspicion. Thanks.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by C3Pnuts
    I know you're not talking pointers in your example but just to make sure you realize this, const char *Foo() is not the same as char * const Foo().

    The first is a normal pointer to a constant char string (pointer can be changed but not the string being pointed to).
    The second is a constant pointer to a normal char string (string being pointed to can be changed but not the pointer).

    A subtle difference.
    And there is everyone's favorite mind twister:
    const char* const Foo()
    aka
    char const* const Foo()



    Just felt like being obnoxious.
    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    [qoute]And there is everyone's favorite mind twister:
    const char* const Foo()
    aka
    char const* const Foo()
    [/qoute]
    Were you just kidding or does it rea;lly do something???
    *Brain system shutting down...*

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    These are identical. They read: "X is a pointer to a constant character". The pointer itself is not constant.
    const char * X
    char const * X

    Now that there's a const on the other side of the star, the pointer itself is also constant. It reads: "X is a constant pointer to a constant character".
    const char * const X

    gg

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Were you just kidding or does it rea;lly do something???
    *Brain system shutting down...*
    1) You can declare a pointer to a const. That prevents you from changing the value at the address stored in the pointer:
    Code:
    int x = 10;
    int const* ptr1 = &x;
    cout<<*ptr1<<endl;
    
    *ptr1 = 20; //error
    However, you can assign another address to the pointer:
    Code:
    int y = 20;
    ptr1 = &y;
    cout<<*ptr1<<endl;
    2) You can declare a const pointer. That means the address stored in the pointer can't be changed:
    Code:
    int a = 30;
    int* const ptr2 = &a;
    cout<<*ptr2<<endl;
    
    int b = 40;
    ptr2 = &b; //error
    However, you can change the value at that address:
    Code:
    *ptr2 = 40;
    cout<<*ptr2<<endl;
    3) You can declare a const pointer to a const. Than means the value at the address stored in the pointer cannot be changed, and the pointer cannot be assigned a different address:
    Code:
    int s = 50;
    const int* const ptr3 = &s;
    cout<<*ptr3<<endl;
    int t = 60;
    
    *ptr3 = t;  //error
    ptr3 = &t; //error
    4) You can declare a const member function. That means the member function is not allowed to change the calling object:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Apple
    {
    private:
    	int size;
    
    public:
    	Apple(int s)
    	{
    		size = s;
    	}
    	void change_size() const
    	{
    		size = 30;
    	}
    };
    
    int main()
    {
    	Apple a(3);
    	a.change_size(); //error
    
    	return 0;
    }
    5) You can declare a const member function that returns a const pointer to a const:

    const int* const someFunc() const

    6) You can declare a const member function that returns a const pointer to a const, and has a parameter that is a const pointer to a const:

    const int* const someFunc(const int* const ptr) const
    Last edited by 7stud; 07-27-2005 at 11:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 03:13 PM
  5. Replies: 6
    Last Post: 12-06-2005, 09:23 AM