473,386 Members | 1,720 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.

Once virtual, always virtual ?!?

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
Jul 22 '05 #1
8 3435
Wouter Moors wrote:
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


Jul 22 '05 #2
Wouter Moors wrote:
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
Jul 22 '05 #3

"Wouter Moors" <wo****@jtag.nl> wrote in message news:77**************************@posting.google.c om...
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).

Jul 22 '05 #4

"Wouter Moors" <wo****@jtag.nl> wrote in message
news:77**************************@posting.google.c om...
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.
Jul 22 '05 #5
Ron Natalie wrote:

"Wouter Moors" <wo****@jtag.nl> wrote in message news:77**************************@posting.google.c om...
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).


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

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 22 '05 #6


jeffc wrote:
"Wouter Moors" <wo****@jtag.nl> wrote in message
news:77**************************@posting.google.c om...
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.


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.
Jul 22 '05 #7

"lilburne" <li******@godzilla.com> wrote in message
news:bv*************@ID-179504.news.uni-berlin.de...

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.


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.
Jul 22 '05 #8
jeffc wrote:
"lilburne" <li******@godzilla.com> wrote in message
news:bv*************@ID-179504.news.uni-berlin.de...

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.


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

Jul 22 '05 #9

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

Similar topics

19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
23
by: ctick | last post by:
A reason for declaring a "virtual destructor" for a Base class is to make sure the destructor of Derived class will be invoked when a pointer of Base type is used to delete an object of Derived. ...
7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
12
by: Daniel Kay | last post by:
Hi Folks! Everytime I work with virtual and pure virtual methods I ask myself if there is a difference between class B and class C (see below). Is it redundant to repeat the virtual keyword in a...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
5
by: Action | last post by:
does it works like ordinary virtual method?? coz I find that child class can't invoke the event of the parent class. class parent { public virtual event SomeDelegate SomeChanged; } class...
4
by: cwc5w | last post by:
I have two classes. One with a regular destructor and the other with a virtual destructor. e.g. class x { ~x(){} } vs
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.