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

virtual question

hi all,
i have 2 classes and a virtual function:
class BaseClass
{ public:
virtual void somefunction();
};

class DerivedClass : public BaseClass
{ public:
/*virtual*/ void somefunction();
};

This seems to work even when I drop the 'virtual' keyword in the derived
class. Does this make any difference? Or is the second 'virtual' in the
derived class obsolete?

Thanks,
Thomas

Apr 26 '06 #1
7 1365
There isn't any difference, in the sense that both are legal and
mean the same thing. On the other hand, a programmer looking
at the DerivedClass will have to wonder wether somefunction is
virtual or not. A bit of a nuissance when dealing with C++ code.
Thomas wrote:
hi all,
i have 2 classes and a virtual function:
class BaseClass
{ public:
virtual void somefunction();
};

class DerivedClass : public BaseClass
{ public:
/*virtual*/ void somefunction();
};

This seems to work even when I drop the 'virtual' keyword in the derived
class. Does this make any difference? Or is the second 'virtual' in the
derived class obsolete?

Thanks,
Thomas


Apr 26 '06 #2

"Thomas" <le****@gmx.de> wrote in message
news:44***********************@newsread4.arcor-online.net...
hi all,
i have 2 classes and a virtual function:
class BaseClass
{ public:
virtual void somefunction();
};

class DerivedClass : public BaseClass
{ public:
/*virtual*/ void somefunction();
};

This seems to work even when I drop the 'virtual' keyword in the derived
class. Does this make any difference? Or is the second 'virtual' in the
derived class obsolete?


"Obsolete" isn't the word I would use. Remember this: "once virtual, always
virtual." Therefore, while the use of "virtual" in derived classes is
allowed, it is redundant and unnecessary.

- Dennis
Apr 26 '06 #3

"Dennis Jones" <no****@nospam.com> wrote in message
news:rEM3g.8227$BO2.2605@trnddc02...

"Obsolete" isn't the word I would use. Remember this: "once virtual, always virtual." Therefore, while the use of "virtual" in derived classes is
allowed, it is redundant and unnecessary.


Oh, I should point out that while including "virtual" in derived classes
does no harm, it certainly makes it clear that the function is virtual.
Otherwise, a reader who does not look at the base class would have no way of
knowing that "somefunction" was virtual. Some people prefer to use a
variation of Hungarian notation to indicate the "virtual-ness" of a function
(e.g. "vSomeFunction"), but I wouldn't advocate this.

What's interesting to me is that the language does not allow you to include
the "virtual" keyword in the definition, only in the declaration. So much
for consistency!

- Dennis
Apr 26 '06 #4
Dennis Jones wrote:

Oh, I should point out that while including "virtual" in derived classes
does no harm, it certainly makes it clear that the function is virtual.
Otherwise, a reader who does not look at the base class would have no way of
knowing that "somefunction" was virtual.


However, marking it "virtual" in the derived class won't help this
clueless programmer to know what its purpose is, because without looking
at the documentation for the base class there is no way to know that it
is defined in the base class and overridden in the derived class.

--

Pete Becker
Roundhouse Consulting, Ltd.
Apr 26 '06 #5
Thomas wrote:
hi all,
i have 2 classes and a virtual function:
class BaseClass
{ public:
virtual void somefunction();
};

class DerivedClass : public BaseClass
{ public:
/*virtual*/ void somefunction();
};

This seems to work even when I drop the 'virtual' keyword in the derived
class. Does this make any difference? Or is the second 'virtual' in the
derived class obsolete?

It's not necessarily obsolete, but it is redundant. Once it's virtual
in the base, it's virtual in the children.
Apr 26 '06 #6
ok, got it. Thanks everybody
Apr 26 '06 #7
Dennis Jones wrote:
Oh, I should point out that while including "virtual" in derived
classes does no harm, it certainly makes it clear that the function
is virtual. Otherwise, a reader who does not look at the base class
would have no way of knowing that "somefunction" was virtual.
But 'virtual' on the derived class function doesn't mean that it's virtual in the base
class, although you would normally expect it to be. It only means that it's virtual for
that class and its derived classes. If you hope to use base-class pointers pointers
polymorphically, you still need to check the base class to be sure which members are
virtual.
Some
people prefer to use a variation of Hungarian notation to indicate
the "virtual-ness" of a function (e.g. "vSomeFunction"), but I
wouldn't advocate this.
Yuk.
What's interesting to me is that the language does not allow you to
include the "virtual" keyword in the definition, only in the
declaration. So much for consistency!


Well, it's always redundant for the definition. It's the same for class static functions.

DW
Apr 26 '06 #8

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

Similar topics

8
by: Wouter Moors | last post by:
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...
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
11
by: Bonj | last post by:
Hello, Can anyone help me with these fairly simple questions. 1) What is the point in virtual destructors - I've heard it's a good thing for base-classes, but what are the advantages and...
4
by: aap | last post by:
Hi, I have the following code. #include <iostream> using namespace std; class Root { public: virtual void Root1() = 0; virtual void Root2() = 0;
5
by: gouqizi.lvcha | last post by:
Hi, all: I have 3 class X, Y, Z class Y is a subclass of class X; class Z is a subclass of class Y; i.e. class Y : public class X class Z : public class Y
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary,...
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
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
4
by: archimed7592 | last post by:
Hi. for example i have base class A and dirved class B: struct A { virtual void f() { std::cout << "A::f()" << std::endl; } }; struct B : public A {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.