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
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
>> 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().Originally Posted by Codeplug
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.
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.
7stud:
I knew my audience would know I spoke of a member function.
Codeplug confirmed my suspicion. Thanks.
And there is everyone's favorite mind twister:Originally Posted by C3Pnuts
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.
[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...*
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
1) You can declare a pointer to a const. That prevents you from changing the value at the address stored in the pointer:Were you just kidding or does it rea;lly do something???
*Brain system shutting down...*
However, you can assign another address to the pointer:Code:int x = 10; int const* ptr1 = &x; cout<<*ptr1<<endl; *ptr1 = 20; //error
2) You can declare a const pointer. That means the address stored in the pointer can't be changed:Code:int y = 20; ptr1 = &y; cout<<*ptr1<<endl;
However, you can change the value at that address:Code:int a = 30; int* const ptr2 = &a; cout<<*ptr2<<endl; int b = 40; ptr2 = &b; //error
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:*ptr2 = 40; cout<<*ptr2<<endl;
4) You can declare a const member function. That means the member function is not allowed to change the calling object:Code:int s = 50; const int* const ptr3 = &s; cout<<*ptr3<<endl; int t = 60; *ptr3 = t; //error ptr3 = &t; //error
5) You can declare a const member function that returns a const pointer to a const: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; }
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.