Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling non const function from a const function?

Reply
Thread Tools

Calling non const function from a const function?

 
 
none
Guest
Posts: n/a
 
      05-19-2010
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;
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-19-2010
On 5/19/2010 2:41 PM, none wrote:
> 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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      05-19-2010
On May 19, 9:41*pm, none <""mort\"@(none)"> wrote:
> 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.

> * *}
> * *void testNonConst() {
> * *}
>
> private:
>
> };
>
> int main(){
> * *Test t;
> * *t.testConst();
> * *return 0;
> }


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-20-2010
On May 19, 7:52 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> On 5/19/2010 2:41 PM, none wrote:
> > 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'...


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.

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
call const function from non const function Fabian Wein C++ 8 07-26-2007 11:51 AM
Overriding const member function with non-const David Scarlett C++ 3 02-07-2006 01:34 PM
is const function faster than non-const? Yuming Ma C++ 5 12-08-2003 03:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57