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

T

todd smith

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
 
B

bartek

(e-mail address removed) (todd smith) wrote in
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.
 
V

Victor Bazarov

todd said:
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
 
K

Karl Heinz Buchegger

todd said:
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.
 
D

David Harmon

On 2 Jun 2004 07:49:54 -0700 in comp.lang.c++, (e-mail address removed) (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.
 
J

John Carson

todd smith said:
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
 
T

todd smith

bartek said:
(e-mail address removed) (todd smith) wrote in


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top