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

a basic virtual function question

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

X has a virtual function, for example, print(),
Y and Z both need to override this function.

My question is : Need I declare print as a virtual in class Y also?
I write a code and it seems that no need to declare virtual in class
Y's print method, but I don't know why. Can anyone help me a bit, thank
you.

------------------Code snippet------------------------------------

#include <iostream>
using namespace std;

class X
{
public:
virtual void print()
{
cout << "class X\n";
}
};

class Y :public X
{
public:
virual void print()
~~~~~~
{
cout << "class Y\n";
}
};

class Z :public Y
{
public:
void print()
{
cout << "class Z\n";
}
};

------------------------------------------------------------
Rick

Oct 25 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

go***********@gmail.com wrote:
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

X has a virtual function, for example, print(),
Y and Z both need to override this function.

My question is : Need I declare print as a virtual in class Y also?
No. The virtual property of a function is, unfortunately, silently
inherited from bases all throughout the derived class hierarchy.

This means that if you introduce a new virtual function to a
widely-inherited base class, and its name and type signature happens to
match existing functions in some of the derived classes, you have
probably just introduced a bug, and one that may be hard to find. These
existing functions now become virtuals that override the ``new''
function, no matter how distant they are in the inheritance tree.
I write a code and it seems that no need to declare virtual in class
Y's print method, but I don't know why.


It's an inheritance rule.

Oct 25 '05 #2

P: n/a
If you want to override the print function of class X, you don't need
to use 'virtual' keyword explicitly in the derived class. Because the
overriding function defautly is a virtual function, no matter it has
'virtual' keyword or not. I think, 'virtual function' means that , the
address of the calling function can only be gotten during run-time, not
compile-time.

Oct 25 '05 #3

P: n/a
go***********@gmail.com wrote:
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

X has a virtual function, for example, print(),
Y and Z both need to override this function.

My question is : Need I declare print as a virtual in class Y also?
I write a code and it seems that no need to declare virtual in class
Y's print method, but I don't know why. Can anyone help me a bit,
thank you.

------------------Code snippet------------------------------------

#include <iostream>
using namespace std;

class X
{
public:
virtual void print()
You only need the 'virtual' keyword here.
{
cout << "class X\n";
}
};

class Y :public X
{
public:
virual void print()
~~~~~~
Here you can have:
virtual void print()
or:
void print()

Either way it is virtual because it overrides the virtual X::print.
{
cout << "class Y\n";
}
};

class Z :public Y
{
public:
void print()
The same goes here.
{
cout << "class Z\n";
}
};


All overrides of a virtual function are implicitly virtual as well, with or
without the keyword. This is probably because it does not make much sense to
allow an override of a virtual function to itself be non-virtual, so the
compiler automatically makes it virtual and doesn't make a fuss if you
didn't use the keyword. Some people prefer to use the keyword on all virtual
functions, simply so you don't have to look up the one at the top to find
out if it's virtual, but that's up to you.

DW
Oct 25 '05 #4

P: n/a
Kaz Kylheku wrote:
go***********@gmail.com wrote:
X has a virtual function, for example, print(),
Y and Z both need to override this function.

My question is : Need I declare print as a virtual in class Y also?


No. The virtual property of a function is, unfortunately, silently
inherited from bases all throughout the derived class hierarchy.

This means that if you introduce a new virtual function to a
widely-inherited base class, and its name and type signature happens
to match existing functions in some of the derived classes, you have
probably just introduced a bug, and one that may be hard to find.


Kind of an unlikely bug isn't it? Same name, same parameter types by
accident? And even if you do this without realizing it, isn't it virtually
(pun intended) certain that you'll want the most-derived function to be
called, should the call be made through a base-class reference or pointer?

DW
Oct 25 '05 #5

P: n/a
function once declared virtual is always virtual in its class
hierarchy. More interstingly even if the same function is made private
it can still be accessed as its virtual.
class X
{
public:
virtual void show()
{
cout<<"X::show()"<<endl;
}
};

class Y : public X
{
//Make this function private
private:
void show()
{
cout<<"Y::show()"<<endl;
}
};

void main()
{
X *pX = new Y;
pX->show();
}

Output:
----------
Y::show().

Oct 25 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.