Calling non const function from a const function?

N

none

I have extended a library class 'LibraryBase' which I cannot modify. 'LibraryBase' contains a const
function 'ConstFunction'. In my subclass 'LibrarySub' I am overriding 'ConstFunction'. But in this
function I now need to call a non const function.

But that gives an error and as I understand also violates the const principle.

But is there no way to call a non const function from a const function? Here is a basic example:

class Test {
public:
void testConst() const {
// this is a no go!
testNonConst();
}
void testNonConst() {
}

private:
};

int main(){
Test t;
t.testConst();
return 0;
}
 
V

Victor Bazarov

I have extended a library class 'LibraryBase' which I cannot modify.
'LibraryBase' contains a const function 'ConstFunction'. In my subclass
'LibrarySub' I am overriding 'ConstFunction'. But in this function I now
need to call a non const function.

But that gives an error and as I understand also violates the const
principle.

But is there no way to call a non const function from a const function?
Here is a basic example:

class Test {
public:
void testConst() const {
// this is a no go!
testNonConst();
}
void testNonConst() {
}

private:
};

int main(){
Test t;
t.testConst();
return 0;
}

The only legal way is to do a const_cast on 'this'. And only do it if
you know that your object is non-const, which you probably can't know
since the system is going to be calling your 'ConstFunction'...

Why do you need to call the non-const function? The whole point in
having the 'const' in a function is to promise that the state of the
object is not going to change during the call. You're violating that
promise. Could it be you're overriding a wrong function?

Another possible solution is declaring your data that you need to change
as 'mutable' (RTFM). Then you can change the value of such data in a
member function declared 'const'. Not that I advocate that.

V
 
Ö

Öö Tiib

I have extended a library class 'LibraryBase' which I cannot modify. 'LibraryBase' contains a const
function 'ConstFunction'. In my subclass 'LibrarySub' I am overriding 'ConstFunction'. But in this
function I now need to call a non const function.

Like you have already told you are developing/maintaining something on
base of something that should not compile. It may do anything. Nothing
is granted. It is *lot* worse situation than to write directly in
assembler. I would ask for budget to rewrite it and if refused, i
would turn it down. World is full of lot more hopeful job not done.
But that gives an error and as I understand also violates the const principle.

But is there no way to call a non const function from a const function? Here is a basic example:

class Test {
public:
   void testConst() const {
     // this is a no go!
     testNonConst();

You may try silly things like:

const_cast<Test*>(this)->testNonConst();

But i would prefer to run fast. There is good life elsewhere waiting
for you.
 
J

James Kanze

The only legal way is to do a const_cast on 'this'. And only
do it if you know that your object is non-const, which you
probably can't know since the system is going to be calling
your 'ConstFunction'...

There's no problem calling a non-const function on a const
object, as long as the function doesn't actually modify the
object in any way.
Why do you need to call the non-const function?

Probably because the class in question isn't const-correct. He
says he can't modify it, which means that he probably got it
from somewhere else, where they didn't worry about const.
The whole point in having the 'const' in a function is to
promise that the state of the object is not going to change
during the call. You're violating that promise. Could it be
you're overriding a wrong function?

He's only violating the promess if the base class function
actually does modify something. The original poster, you and
I know enough to make this promess explicit with the const
keyword, but there are doubtlessly still a lot of people around
who don't know that---including, perhaps, the author of the
original class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top