Once virtual, always virtual ?!?

B

Bob Smith

Wouter said:
Hi all,

i got a question about virtual functions.
Suppose class A has a virtual function doIt() and in my derived class
B i do not declare it as virtual. Now, will that function still be
virtual in a third class C which is derived from class B ?
yes,
/G



TAI,

Wouter
 
W

Wouter Moors

Hi all,

i got a question about virtual functions.
Suppose class A has a virtual function doIt() and in my derived class
B i do not declare it as virtual. Now, will that function still be
virtual in a third class C which is derived from class B ?

TAI,

Wouter
 
A

AirPete

Wouter said:
Hi all,

i got a question about virtual functions.
Suppose class A has a virtual function doIt() and in my derived class
B i do not declare it as virtual. Now, will that function still be
virtual in a third class C which is derived from class B ?

TAI,

Wouter

Yes, it will.

- Pete
 
R

Ron Natalie

Wouter Moors said:
Hi all,

i got a question about virtual functions.
Suppose class A has a virtual function doIt() and in my derived class
B i do not declare it as virtual. Now, will that function still be
virtual in a third class C which is derived from class B ?

Functions with the same name and parameter types in derived classes will be
virtual if they are declared virtual in the base class. There's no way to
stop this virtual propagation (you didn't ask, but I'll anticipate your next question).
 
J

jeffc

Wouter Moors said:
Hi all,

i got a question about virtual functions.
Suppose class A has a virtual function doIt() and in my derived class
B i do not declare it as virtual. Now, will that function still be
virtual in a third class C which is derived from class B ?

It's funny that sometimes even "experienced" C++ programmers will argue with
me about this. Something doesn't work, they'll see the lack of the
"virtual" keyword in the subclass, and try to tell me the problem is because
it's missing.
 
D

David Rubin

Ron said:
Functions with the same name and parameter types in derived classes will be
virtual if they are declared virtual in the base class. There's no way to
stop this virtual propagation (you didn't ask, but I'll anticipate your next question).

It might be easier from a maintenance perspective to declare the
function virtual in the derived classes, although there is a tradeoff
here if you later decide not to make the function virtual in the base
class.

/david
 
L

lilburne

jeffc said:
It's funny that sometimes even "experienced" C++ programmers will argue with
me about this. Something doesn't work, they'll see the lack of the
"virtual" keyword in the subclass, and try to tell me the problem is because
it's missing.

We have a simple interview question concerning three classes Base,
Derived1, and Derived2. With a virtual function, and virtual destructor.
Question is "What does this print?" The answers can be very strange, but
basically a large portion of those questioned think that leaving off
virtual in the derived class makes it none virtual.

In telephone interviews I conducted to the question "When did you last
design a class heirarchy?" two out of three responded "I've never done
that!" Our conclusions are that a number of 'C++ programmers' have never
derived a class heirarchy in C++ and mostly write code to interact with
some database API.
 
J

jeffc

lilburne said:
We have a simple interview question concerning three classes Base,
Derived1, and Derived2. With a virtual function, and virtual destructor.
Question is "What does this print?" The answers can be very strange, but
basically a large portion of those questioned think that leaving off
virtual in the derived class makes it none virtual.

Well, in any case, I always like to be explicit. I put the virtual there,
and I always use the default public modifier in structs and private in
classes. It could be that it so happens that they were taught the same way,
but forgot that it didn't matter, so with those keywords missing it doesn't
look right to them.
 
L

lilburne

jeffc said:
Well, in any case, I always like to be explicit. I put the virtual there,
and I always use the default public modifier in structs and private in
classes. It could be that it so happens that they were taught the same way,
but forgot that it didn't matter, so with those keywords missing it doesn't
look right to them.

Here too. Coding standard mandates that virtual is
propagated to derived classes that override the function.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top