Wednesday, January 30, 2008

const object and const member function

const object ensure that no data members of the object are changed during the object’s lifetime. It can easily ensure that no public data is modified, but how is it to know which member functions will change the data and which ones are “safe” for a const object? -- use const member function.

For const member function, note that the const keyword must be repeated in the definition (declaration first), or the compiler sees it as a different function.

A const member function is safe to call with both const and non-const objects.

Look at former post Const and Constant Function for more information.

add some code here: such like class A{public: void foo();};const A a; a.foo();//wrong

No comments:

Post a Comment