Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const and no const methods in c++?

I have a program and many of its classes have some operators and methods with the keyword const like the followings:

operator const char* () const;
operator char* ();
void Save(const char *name) const;
void Load(const char *name);

First: what does it mean const at the end of the method declaration?, is it the same like putting it at the beginning?

Second: Why would be a const version and a no const version of operator() needed?

Thanks in advance.

like image 782
nacho4d Avatar asked Oct 15 '10 15:10

nacho4d


People also ask

What are const methods?

The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object.

Can const methods be used by non-const objects?

const member functions may be invoked for const and non-const objects. non-const member functions can only be invoked for non-const objects. If a non-const member function is invoked on a const object, it is a compiler error.

What is const function in C?

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.

Can you pass a non-const to a const?

For instance, you can pass non-const variables to a function that takes a const argument. The const-ness of the argument just means the function promises not to change it, whether or not you require that promise.


2 Answers

First: what does it mean const at the end of the method declaration?, is it the same like putting it at the beginning?

No. A const at the end means that the method may be called on objects that are declared const. A const at the beginning means that the returned value is const.

Second: Why would be a const version and a no const version of operator() needed?

The non-const version returns a char* which is not const. By modifying this char* you could then in fact modify the object (assuming the char* is a member of the object).

Since this is not allowed for const objects, there's an overload of operator() for const objects, so that a const char* is returned, so the object can't be modified through it.

like image 196
sepp2k Avatar answered Sep 20 '22 05:09

sepp2k


'const' at the end tells the compiler that this method does not change any member variables - that it is safe to call this method on const instances. So, Save could be called on a const instance, since it won't change that instance. Load on the other hand, will change the instance so can't be used on const instances.

The const version of operator() passes back a const pointer, guaranteeing the buffer passed back won't change. Presumably that's a pointer into a instance variable of the class. For non-const instances, the other operator() passes back a non-const pointer. It would have to be a pointer to some memory that even if written to, wouldn't change the contents of the instance.

Also, look up the 'mutable' keyword sometime. Understanding that will help you understand this idea of const-correctness.

like image 44
Graham Perks Avatar answered Sep 21 '22 05:09

Graham Perks