Archived

This topic is now archived and is closed to further replies.

a const situation....

This topic is 5516 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok here''s one. we all know const int number; but what does "const" mean in these sequences? const int class::addNumber(); int class::addNumber()const; int class::addNumber (const int); and my favorite stumper: const int class::addNumber(const int) const; so how do they work/mean?

Share this post


Link to post
Share on other sites
const int Class::addNumber();

addNumber returns a constant integer - here the const isn''t very useful since it returns the result by value.

int Class::addNumber() const;

The addNumber member function itself is declared const - it is not allowed to modify the Class object it is a member of, but in exchange is allowed to be called even if this object is constant. (In practice, it makes the this pointer constant)

Typical example :

const Class& Class::SetValue( int ); // called only on non-const Class objects - it modifies them
int Class::GetValue() const; // can be called on any Class object - it guarantees not to modify them


int class::addNumber (const int);

The parameter you pass to the member function is a constant. You will not be allowed to modify it in the function. Not very useful since you''re passing by value - more interesting when you pass by reference.


const int class::addNumber(const int) const;

All of the above, combined - the return value is constant, the object is constant, the parameter is constant.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites