a question on function declaration
| winecoding (26) |
|
In the following class definition:
1 2 3 4 5
|
class X{
// ...
int m1( );
int m2( ) const;
}
|
what is the difference between function m1 and m2, or what is affected by adding a const to the declaration of m2?
|
|
|
| Intrexa (292) |
|
|
A constant function does not modify the object for which it was called.
|
|
|
| Bazzy (6275) |
|
If you have a const object/reference to type X you can only call const methods.
The const qualifier can be used to overload a method
1 2 3 4 5
|
class X{
// ...
int m( ); // called by non-const objects
int m( ) const; // called by const objects
};
|
|
|
|
Topic archived. No new replies allowed.