About the constness of a class

Hi guys, I have a question about the constness of a class.

If I declare a class to be const, can I access to its member function which may change its value?

For example:

1
2
3
4
5
6
7
8
9
10
class Point{
public:
     Point(int x, int y);
     void setX(int newvalue);
};

void main(){
     const Point a(1,3);
     a.setX(100);
}


Can I do that?
Last edited on
No. The fact that you declared a const means you can't modify a's contents.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
First, a correction in terminology. A class can't be const. const Point a(1,3); declares a constant object, or constant instance of the class Point.

Second, no. Non-const member functions can't be called on const instances. const member functions can, but they are not allowed to modify the contents of the instance.
Thank you very much guys!

I'm new to the forum and it's my first posted topic.

My next topic will highlight my code.
Topic archived. No new replies allowed.