473,386 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Overriding const member function with non-const

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
Feb 7 '06 #1
3 8591
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...
Feb 7 '06 #2
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.
Feb 7 '06 #3
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

Feb 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wenjie | last post by:
Hello, I read someone posted assertions that even the (public) member function is not static, there are probably only one copy of the code in the executable. Then except the...
3
by: Philippe Guglielmetti | last post by:
Look at these few lines of code: class A { public: virtual void f() { cout << "A";}}; class B : public A{public: static void f() { cout << "B"; }}; class C : public B{public: void f() { cout <<...
13
by: Erik Haugen | last post by:
From reading gotw#84 (http://www.gotw.ca/gotw/084.htm), I'm convinced that I should try to make functions nonfriend nonmembers when practical, but then I came across this: Bruce Eckel says about...
3
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot...
11
by: Michael | last post by:
Hi, As an exercise, I created a class C that duplicates a value x thus having x_val1 and x_val2 I also overrided the + operator so that if I have z = x + y (x,y and z belong class C) then + ...
3
by: Vajira | last post by:
Lets say there is a inheritance heirarchy like this. C3 inherit from C2 and C2 inherit from C1 ( C3 -> C2 -> C1 ). If C1 class has a public virtual member function call 'Remove()', can I limit...
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
6
by: rlvladbob | last post by:
Hi, I've try to access a static method using an instance instead of a class. public class test{ public static void ShowAText(string ThisText) { System.Console.WriteLine("->{0}",ThisText); }
22
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.