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.
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.
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.
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.
Yes, in the sense that it can be const_cast away, or the object can have mutable members.
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".
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 ...
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.
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.
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With