Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Overriding const member function with non-const

Reply
Thread Tools

Overriding const member function with non-const

 
 
David Scarlett
Guest
Posts: n/a
 
      02-07-2006
Hi all,
I've got a question regarding overriding const member functions with
non-const functions.

Let's say I've got a base class defined as follows:

/*******************/
class Foo
{
public:
Foo();
virtual ~Foo();

virtual void Run() const;
virtual void DoSomething();
}
/*******************/

Now, what I want to do is create another class derived from it, and
override the Run() method. However, I will need to call DoSomething()
from this new version of Run(), even though Run() is const and
DoSomething() is not. I'm not concerned about whether I should be
modifying a const object, because the only non-const behaviour
DoSomething() exhibits is logging related.

So here are two ways I can think of doing it. Both compile under
Microsoft VC++6, but I'm still wondering whether the first method in
particular is ISO C++ compatible (as I wasn't expecting it to
compile)...

/*******************/
class Bar1 : public Foo
{
public:
Bar1();
virtual ~Bar1();

// Overriding const Run() with non-const method.
virtual void Run() {DoSomething();}
virtual void DoSomething();
}

class Bar2 : public Foo
{
public:
Bar2();
virtual ~Bar2();

virtual void Run() const {const_cast<Bar2*>(this)->DoSomething();}
virtual void DoSomething();
}
/*******************/

So do these follow Standard C++, and what are people's opinions on
which is the better way to do it?

Thanks.


--
David Scarlett

dscarlett@_ _ _ _ _ _ _ _
_ _ _ _ _ optusnet.com.au
 
Reply With Quote
 
 
 
 
Ben Pope
Guest
Posts: n/a
 
      02-07-2006
David Scarlett wrote:
> Hi all,
> I've got a question regarding overriding const member functions with
> non-const functions.
>
> Let's say I've got a base class defined as follows:
>
> /*******************/
> class Foo
> {
> public:
> Foo();
> virtual ~Foo();
>
> virtual void Run() const;
> virtual void DoSomething();
> }
> /*******************/
>
> Now, what I want to do is create another class derived from it, and
> override the Run() method. However, I will need to call DoSomething()
> from this new version of Run(), even though Run() is const and
> DoSomething() is not. I'm not concerned about whether I should be
> modifying a const object, because the only non-const behaviour
> DoSomething() exhibits is logging related.


So make DoSomething const as well, and make whatever it changes to be
mutable.

> So here are two ways I can think of doing it. Both compile under
> Microsoft VC++6, but I'm still wondering whether the first method in
> particular is ISO C++ compatible (as I wasn't expecting it to
> compile)...
>
> /*******************/
> class Bar1 : public Foo
> {
> public:
> Bar1();
> virtual ~Bar1();
>
> // Overriding const Run() with non-const method.
> virtual void Run() {DoSomething();}
> virtual void DoSomething();
> }


I don't think that would work if you're dealing with a base pointer.

> class Bar2 : public Foo
> {
> public:
> Bar2();
> virtual ~Bar2();
>
> virtual void Run() const {const_cast<Bar2*>(this)->DoSomething();}
> virtual void DoSomething();
> }
> /*******************/


I'm not sure that would result in defined behaviour.

> So do these follow Standard C++, and what are people's opinions on
> which is the better way to do it?


See above about mutable, that way you're not lying about the constness
to the compiler or the user. (maybe you're misleading the user a little)

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-07-2006
David Scarlett wrote:

> Hi all,
> I've got a question regarding overriding const member functions with
> non-const functions.


That's not possible.

> Let's say I've got a base class defined as follows:
>
> /*******************/
> class Foo
> {
> public:
> Foo();
> virtual ~Foo();
>
> virtual void Run() const;
> virtual void DoSomething();
> }
> /*******************/
>
> Now, what I want to do is create another class derived from it, and
> override the Run() method. However, I will need to call DoSomething()
> from this new version of Run(), even though Run() is const and
> DoSomething() is not. I'm not concerned about whether I should be
> modifying a const object, because the only non-const behaviour
> DoSomething() exhibits is logging related.
>
> So here are two ways I can think of doing it. Both compile under
> Microsoft VC++6, but I'm still wondering whether the first method in
> particular is ISO C++ compatible (as I wasn't expecting it to
> compile)...
>
> /*******************/
> class Bar1 : public Foo
> {
> public:
> Bar1();
> virtual ~Bar1();
>
> // Overriding const Run() with non-const method.
> virtual void Run() {DoSomething();}
> virtual void DoSomething();
> }


Are you sure that this overrides the base class Run? This would be quite a
severe error. But then, VC++6 isn't really famous for its standard
compliance.

> class Bar2 : public Foo
> {
> public:
> Bar2();
> virtual ~Bar2();
>
> virtual void Run() const {const_cast<Bar2*>(this)->DoSomething();}
> virtual void DoSomething();
> }
> /*******************/
>
> So do these follow Standard C++, and what are people's opinions on
> which is the better way to do it?


That depends. You shouldn't look so much at what actually modifies data and
what doesn't. Take a step backwards and look at the "logical constness".
From the user's (of the class) point of view, does DoSomething() change the
object or not? If it does, it should stay non-const. However, then Run()
potentially changes it too, so it should be non-const as well.
If DoSomething() doesn't change the external appearance of the object, make
it const. If it needs to change some caching or logging data within the
object, make that data mutable.


 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      02-07-2006
David Scarlett wrote:
> Hi all,
> I've got a question regarding overriding const member functions with
> non-const functions.
>
> Let's say I've got a base class defined as follows:
>
> /*******************/
> class Foo
> {
> public:
> Foo();
> virtual ~Foo();
>
> virtual void Run() const;
> virtual void DoSomething();
> }
> /*******************/
>
> Now, what I want to do is create another class derived from it, and
> override the Run() method. However, I will need to call DoSomething()
> from this new version of Run(), even though Run() is const and
> DoSomething() is not. I'm not concerned about whether I should be
> modifying a const object, because the only non-const behaviour
> DoSomething() exhibits is logging related.
>
> So here are two ways I can think of doing it. Both compile under
> Microsoft VC++6, but I'm still wondering whether the first method in
> particular is ISO C++ compatible (as I wasn't expecting it to
> compile)...
>
> /*******************/
> class Bar1 : public Foo
> {
> public:
> Bar1();
> virtual ~Bar1();
>
> // Overriding const Run() with non-const method.
> virtual void Run() {DoSomething();}
> virtual void DoSomething();
> }
>
> class Bar2 : public Foo
> {
> public:
> Bar2();
> virtual ~Bar2();
>
> virtual void Run() const {const_cast<Bar2*>(this)->DoSomething();}
> virtual void DoSomething();
> }
> /*******************/
>
> So do these follow Standard C++, and what are people's opinions on
> which is the better way to do it?
>
> Thanks.
>
>
> --
> David Scarlett
>
> dscarlett@_ _ _ _ _ _ _ _
> _ _ _ _ _ optusnet.com.au


See this FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-18.13

Cheers! --M

 
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/non const member function vs. templates Markus Keppeler C++ 13 07-29-2011 02:15 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
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
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments