By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 930 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.