Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a const function from a non-const object

Tags:

I need to call a const function from a non-const object. See example

struct IProcess {    virtual bool doSomeWork() const = 0L; }; class Foo : public IProcess {       virtual bool doSomeWork() const {     ...   } };  class Bar { public:    const IProcess& getProcess() const {return ...;}    IProcess& getProcess() {return ...;}     void doOtherWork {     getProcess().doSomeWork();           } }; 

Calling

getProcess().doSomeWork(); 

will always results in a call to

IProcess& getProcess() 

Is there another way to call

const IProcess& getProcess() const  

from a non constant member function? I have so far used

const_cast<const Bar*>(this)->getProcess().doSomeWork(); 

which does the trick but seems overly complicated.


Edit: I should mention that code is being refactored and eventually only one function will remain.

const IProcess& getProcess() const  

However, currently there is a side effect and the const call may return a different instance of IProcess some of the time.

Please keep on topic.

like image 842
Chris Avatar asked Jan 02 '09 15:01

Chris


People also ask

Can a non-const object call a const function?

const function use cases A const function can be called by either a const or non- const object. Only a non- const object can call a non- const function; a const object cannot call it.

Can a constant member function call a non-constant member function?

Any attempt to change a data member of the object that called a constant method will result in a syntax error, as will attempting to call a non- const member function for that object. Constant objects can only call constant member functions. Non-constant objects can call both constant and non-constant member functions.

Can you pass a non-const to a const?

Converting between const and non-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.

Can we invoke non-const member function using a pointer reference to const?

Yes, in the sense that it can be const_cast away, or the object can have mutable members.

Can you call a const function from a non-const function?

A const function can always be called from a non-const function. You probably meant the reverse way: "calling a non-const function from. a const function".

What is a const member function in C++?

Const member functions in C++ 1 When a function is declared as const, it can be called on any type of object, const object as well as non-const objects. 2 Whenever an object is declared as const, it needs to be initialized at the time of declaration. however, the object... More ...

What happens when a function is declared as a const object?

When a function is declared as const, it can be called on any type of object, const object as well as non-const objects. Whenever an object is declared as const, it needs to be initialized at the time of declaration. however, the object initialization while declaring is possible only with the help of constructors.

What is a const function in JavaScript?

A function becomes const when const keyword is used in function’s declaration. The idea of const functions is not allow them to modify the object on which they are called. It is recommended practice to make as many functions const as possible so that accidental changes to objects are avoided. Following is a simple example of const function.


2 Answers

const_cast is for casting away constness!

You're casting from non-const to const which is safe, so use static_cast:

   static_cast<const Bar*>(this)->getProcess().doSomeWork(); 

I mean techincally speaking you can cast in constness with const_cast, but it's not a pragmatic use of the operator. The purpose of new style casts (versus the old c-style cast), is to communicate the intent of the cast. const_cast is a code smell, and it's use should be reviewed at least. static_cast on the other hand is safe. But it's a matter of C++ style.

Or you can create a new (private) const method, and call that from doOtherWork:

  void doSomeWorkOnProcess() const { getProcess().doSomeWork(); } 

Using a const temporary is also an option (answer by "MSN"):

   const Bar* _this = this;    _this->getProcess().doSomeWork(); 
like image 149
mfazekas Avatar answered Nov 06 '22 16:11

mfazekas


Avoid the cast: assign this to a const Bar * or whatever and use that to call getProcess().

There are some pedantic reasons to do that, but it also makes it more obvious what you are doing without forcing the compiler to do something potentially unsafe. Granted, you may never hit those cases, but you might as well write something that doesn't use a cast in this case.

like image 28
MSN Avatar answered Nov 06 '22 14:11

MSN