Jump to content
  • Advertisement
Sign in to follow this  
Jiia

virtual keyword

This topic is 4850 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

Is it necessary to declare inherited functions as virtual as well as the base functions? Example:
class Base
{
public:
  virtual void Draw()=0;
};
class Child : public Base
{
public:
  virtual void Draw();
    .. or just ..
  void Draw();
};
Would there be any difference in functionality between these two? Or does the difference only come up if something is derived from Child? If it works both ways, is it common to include or common to exclude the child's virtual keyword? Sorry if I'm asking dumb questions. I've just been including the keyword in child types since I started programming and have only recently questioned it. Thanks for any help

Share this post


Link to post
Share on other sites
Advertisement
See the following example:

Base* b;
Child c;
b = &c;

b->draw();

with virtual on both here you will get the draw of Child;
without the virtual in c, here would the draw of Base called.

Share this post


Link to post
Share on other sites
It works both ways. Child::Draw inherits the virtual specifier from Base::Draw.

I usually write virtual on Child::Draw however, as it makes it clear that it is a virtual function, even if I do not have the definition of Base handy.

[Edit: Beaten to it by Sparhawk, who is incorrect. In both cases, Child::Draw will be called]

Share this post


Link to post
Share on other sites
In C++: once virtual, always virtual. If a base class declares a member function virtual then any derived class member function which matches that functions signature (except for template member functions) will also be virtual:
#include <iostream>

class Base
{

public:

virtual void function()
{
std::cout << "Base::function\n";
}

};

class Intermediate
:
public Base
{

public:

void function()
{
std::cout << "Intermediate::function\n";
}

};

class Derived
:
public Intermediate
{

public:

void function()
{
std::cout << "Derived::function\n";
}

};

int main()
{
Base b;
Intermediate i;
Derived d;
Base * bpb = &b;
Base * bpi = &i;
Intermediate * ipi = &i;
Base * bpd = &d;
Intermediate * ipd = &d;
Derived * dpd = &d;
Base & brb = b;
Base & bri = i;
Intermediate & iri = i;
Base & brd = d;
Intermediate & ird = d;
Derived & drd = d;
b.function(); // prints "Base::function\n"
i.function(); // prints "Intermediate::function\n"
d.function(); // prints "Derived::function\n"
bpb->function(); // prints "Base::function\n"
bpi->function(); // prints "Intermediate::function\n"
ipi->function(); // prints "Intermediate::function\n"
bpd->function(); // prints "Derived::function\n"
ipd->function(); // prints "Derived::function\n"
dpd->function(); // prints "Derived::function\n"
brb.function(); // prints "Base::function\n"
bri.function(); // prints "Intermediate::function\n"
iri.function(); // prints "Intermediate::function\n"
brd.function(); // prints "Derived::function\n"
ird.function(); // prints "Derived::function\n"
drd.function(); // prints "Derived::function\n"
}

That said, I consider it good practice to put the virtual keyword in derived classes as a form of documentation.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by bakery2k1
It works both ways. Child::Draw inherits the virtual specifier from Base::Draw.

I usually write virtual on Child::Draw however, as it makes it clear that it is a virtual function, even if I do not have the definition of Base handy.

[Edit: Beaten to it by Sparhawk, who is incorrect. In both cases, Child::Draw will be called]


O.K. you are right, I've just tested it...But I think in some other languages(like Java, Object Pascal) it is not given that virtual is inherited from the parent class.

Share this post


Link to post
Share on other sites
I don't think my question could be answered any better.
Enigma, you really went out of your way there. I really appreciate it.

Thanks a lot for the help :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Sparhawk42
O.K. you are right, I've just tested it...But I think in some other languages(like Java, Object Pascal) it is not given that virtual is inherited from the parent class.


In Java every non-static function is virtual. In Object Pascal, you don't use the virtual or dynamic keyword when creating a new definition for an inhertied procedure or function, you use the override keyword, though redeclaring a procedure or function without the override keyword will hide the base class procedure or function.

Share this post


Link to post
Share on other sites
While it is not necessary in C++ to continue declaring methods as virtual when subclassing it's generally considered good practice to get into the habit of doing so. Newer languages such as C# require that you specify the 'override' keyword on methods that override a virtual method in the base class. In C# an error can be generated for an instance method with the same signature of a virtual method declared in the base class (see ECMA-334).

It is generally considered highly desirable to declare your intentions explicitly rather than relying on implicit assumptions (even when tools such as doxygen can make note of the override).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!