Sign in to follow this  

Placement of "virtual"

This topic is 4097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Where should the virtual keyword for a function be placed? In base class declaration / function definition? In derived class declaration / function definition?

Share this post


Link to post
Share on other sites
The only place where it is required is the base class declaration; yet it maybe considered good form to place it in the derived class aswell to show it's virtual to users.

Share this post


Link to post
Share on other sites
Quote:
Original post by snooty
Hi,

Where should the virtual keyword for a function be placed?
In base class declaration / function definition?
In derived class declaration / function definition?


In the base class declaration, and optionally in the derived class declaration
Quote:
section 10.3 §2 of the Holy Standard
If a virtual function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name and the same parameter list as in Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf.


If you use the virtual keyword on the function definition, you'll get a compiler error.

Regards,

Share this post


Link to post
Share on other sites

This topic is 4097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this