Sign in to follow this  
snooty

Placement of "virtual"

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

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