473,320 Members | 1,817 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,320 software developers and data experts.

Is it possible to 'Unvirtualize' a method in a base class

If i have the following class hierarchy:

class Base :
{
virtual void foo();
};

class Derived : public Base
{
void foo();
};

Is it possible under VC6 that Derived::foo() would no longer be
virtual? I was under the assumption that once a function has been
declared virtual in a base its virtualness follows it down the
hierarchy, but have been told otherwise with VC6.

Outside of making a class definition a bit clearer (not having to look
at parent class) i would think the virtual keyword in Derived is
redundant.

/todd
Jul 22 '05 #1
6 1613
to*******@yahoo.com (todd smith) wrote in
news:44**************************@posting.google.c om:
If i have the following class hierarchy:

class Base :
{
virtual void foo();
};

class Derived : public Base
{
void foo();
};

Is it possible under VC6 that Derived::foo() would no longer be
virtual? I was under the assumption that once a function has been
declared virtual in a base its virtualness follows it down the
hierarchy, but have been told otherwise with VC6.

Outside of making a class definition a bit clearer (not having to look
at parent class) i would think the virtual keyword in Derived is
redundant.


A method defined virtual in a base class remains such in every derived
class. Even if you omit the virtual keyword in derived class definition
the method still remains virtual. There's nothing you can do about it,
I'm afraid.

It is considered *very good practice* to leave the keyword 'virtual' in
every derived class definition, regardless of its redundancy. It simply
makes code clearer.

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #2
todd smith wrote:

If i have the following class hierarchy:

class Base :
{
virtual void foo();
};

class Derived : public Base
{
void foo();
};

Is it possible under VC6 that Derived::foo() would no longer be
virtual? I was under the assumption that once a function has been
declared virtual in a base its virtualness follows it down the
hierarchy,
You are right
but have been told otherwise with VC6.


Hard to believe. Can you provide a test program to prove this.
I and many others are using VC6 on a daily base since years. While
this compiler has many shortcommings and bugs, this is not one of
them.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3
todd smith wrote:
If i have the following class hierarchy:

class Base :
{
virtual void foo();
};

class Derived : public Base
{
void foo();
};

Is it possible under VC6 that Derived::foo() would no longer be
virtual?
No.
I was under the assumption that once a function has been
declared virtual in a base its virtualness follows it down the
hierarchy, but have been told otherwise with VC6.
Have been told by whom?
Outside of making a class definition a bit clearer (not having to look
at parent class) i would think the virtual keyword in Derived is
redundant.


Yes, it is.

The only possible error is that you (a) made a typo when defining
the member of the Derived, like

void fooo();

or (b) somehow changed its type, like

void foo() const;

which hides the original virtual member.

Victor
Jul 22 '05 #4
On 2 Jun 2004 07:49:54 -0700 in comp.lang.c++, to*******@yahoo.com (todd
smith) wrote,
Is it possible under VC6 that Derived::foo() would no longer be
virtual? I was under the assumption that once a function has been
declared virtual in a base its virtualness follows it down the
hierarchy, but have been told otherwise with VC6.


No. The C++ standard requires that virtuality is inherited. VC6 has
plenty of compatibility problems, but that is not one of them.

Jul 22 '05 #5
"todd smith" <to*******@yahoo.com> wrote in message
news:44**************************@posting.google.c om
If i have the following class hierarchy:

class Base :
{
virtual void foo();
};

class Derived : public Base
{
void foo();
};

Is it possible under VC6 that Derived::foo() would no longer be
virtual? I was under the assumption that once a function has been
declared virtual in a base its virtualness follows it down the
hierarchy, but have been told otherwise with VC6.

Outside of making a class definition a bit clearer (not having to look
at parent class) i would think the virtual keyword in Derived is
redundant.

/todd

As others have said, you can't unvirtualise a function. You can, however,
get the Base function by using the fully qualified name, e.g.,

Base *ptr = new Derived;
ptr->foo(); // calls the function in Derived
ptr->Base::foo() // calls the function in Base
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #6
bartek <sp******************@o2.pl> wrote in message news:<Xn**********************************@153.19. 251.200>...
to*******@yahoo.com (todd smith) wrote in
news:44**************************@posting.google.c om:
If i have the following class hierarchy:

class Base :
{
virtual void foo();
};

class Derived : public Base
{
void foo();
};

Is it possible under VC6 that Derived::foo() would no longer be
virtual? I was under the assumption that once a function has been
declared virtual in a base its virtualness follows it down the
hierarchy, but have been told otherwise with VC6.

Outside of making a class definition a bit clearer (not having to look
at parent class) i would think the virtual keyword in Derived is
redundant.


A method defined virtual in a base class remains such in every derived
class. Even if you omit the virtual keyword in derived class definition
the method still remains virtual. There's nothing you can do about it,
I'm afraid.

It is considered *very good practice* to leave the keyword 'virtual' in
every derived class definition, regardless of its redundancy. It simply
makes code clearer.


thanks! thats what i had thought. i had just been following bad
practice and left out the virtual keyword in my derives...

adding that rule to my list of best practices
Jul 22 '05 #7

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

Similar topics

2
by: Kapil Khosla | last post by:
Dear all, I am trying to underlying implementation of virtual functions in C++. The way I understand polymorphism is class Base { public: virtual int func(); };
5
by: Dan =o\) | last post by:
given this brief scenario, is this possible somehow? class A { public SomeMethod() { } }
10
by: Julia | last post by:
Hi Please can someone explain this behaviour: I have a MustInherit Base class and a Derived class that Inherits Base and Shadows a method in the base class. If I Dim a variable of type...
5
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
6
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there...
12
by: André | last post by:
Hi, i'm learning working with classes. In class "classbase1", i defined an overridable function. In the class "subclass1", i defined the same function with 'Overrides'. In class "classbase2", i...
1
by: gregory.lielens | last post by:
Hello, We are currently writing python bindings to an existing C++ library, and we encountered a problem that some of you may have solved (or that has found unsolvable :( ): A C++ class...
5
by: Ben | last post by:
Hi, i defined a function in the base class 'ford' and the same function (with different output) in subclass "peugeot". I first put 'Overridable function' in the base class and 'Overrides...
11
by: [rob desbois] | last post by:
Hi all, I have a set of classes which implement the virtual constructor idiom. I had a slicing problem which resulted when I forgot to override the clone() function in a derived class. Is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.