Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using 'const' in class's functions [duplicate]

I've seen a lot of uses of the const keyword put after functions in classes, so i wanted to know what was it about. I read up smth at here: http://duramecho.com/ComputerInformation/WhyHowCppConst.html .

It says that const is used because the function "can attempt to alter any member variables in the object" . If this is true, then should it be used everywhere, because i don't want ANY of the member variables to be altered or changed in any way.

class Class2 { void Method1() const;   int MemberVariable1;}  

So, what is the real definition and use of const ?

like image 649
ggg Avatar asked Jan 28 '10 19:01

ggg


2 Answers

A const method can be called on a const object:

class CL2 { public:     void const_method() const;     void method();  private:     int x; };   const CL2 co; CL2 o;  co.const_method();  // legal co.method();        // illegal, can't call regular method on const object o.const_method();   // legal, can call const method on a regulard object o.method();         // legal 

Furthermore, it also tells the compiler that the const method should not be changing the state of the object and will catch those problems:

void CL2::const_method() const {     x = 3;   // illegal, can't modify a member in a const object } 

There is an exception to the above rule by using the mutable modifier, but you should first get good at const correctness before you venture into that territory.

like image 75
R Samuel Klatchko Avatar answered Oct 14 '22 15:10

R Samuel Klatchko


Others have answered the technical side of your question about const member functions, but there is a bigger picture here -- and that is the idea of const correctness.

Long story short, const correctness is about clarifying and enforcing the semantics of your code. Take a simple example. Look at this function declaration:

bool DoTheThing(char* message); 

Suppose someone else wrote this function and you need to call it. Do you know what DoTheThing() does to your char buffer? Maybe it just logs the message to a file, or maybe it changes the string. You can't tell what the semantics of the call are by just looking at the function declaration. If the function doesn't modify the string, then the declaration is const incorrect.

There's practical value to making your functions const correct, too. Namely, depending on the context of the call, you might not be able to call const-incorrect functions without some trickery. For example, assume that you know that DoTheThing() doesn't modify the contents of the string passed to it, and you have this code:

void MyFunction() {   std::string msg = "Hello, const correctness";   DoTheThing(msg.c_str()); } 

The above code won't compile because msg.c_str() returns a const char*. In order to get this code to compile, you would have to do something like this:

void MyFunction() {   std::string msg = "Hello, const correctness";   DoTheThing(msg.begin()); } 

...or even worse:

void MyFunction() {   std::string msg = "Hello, const correctness";   DoTheThing(const_cast<char*>(msg.c_str())); } 

neither of which, arguably, are 'better' than the original code. But because DoTheThing() was written in a const-incorrect way, you have to bend your code around it.

like image 29
John Dibling Avatar answered Oct 14 '22 14:10

John Dibling