Jump to content
  • Advertisement
Sign in to follow this  
bobster

to virtual or not to virtual

This topic is 5115 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, I have been unable to find a clear explanation of this in any of my documentation. If i declare an abstract base class (call it A) and a concrete class (say B) that is derived from A, should I always declare A's virtual functions as virtual in B also? what is the difference between: (declaring a function virtual in A && declaring that function non-virtual in B) and declaring it virtual in both? In the particular case of a virtual destructor for a base class A, I am unclear on whether the destructor of B should also be declared virtual, and what difference that would make. Any answers would be especially appreciated as I am currently doing this stuff on a test sheet for a job application 8|

Share this post


Link to post
Share on other sites
Advertisement
B's destructor should be declared virtual if B have any other function declared virtual. B's functions should be declared virtual if you want other classes that inherit from B to be able to use them as virtual functions, just like with A->B

edit: don't know if my wording was that good. but if a function is virtual in A, but not in B, then it's B's version is the "final" one. ie. you can't make a new one in class C and except that to be called like ((A*)c)->MyFunc()

Share this post


Link to post
Share on other sites
so then in the case of a virtual destructor, if I'm planning to use B as a concrete leaf class which therefore wont have anything derived from it in the future and it doesnt have any virtual functions, then i shouldn't make B's destructor virtual right?

Share this post


Link to post
Share on other sites
Quote:
Original post by Jolle
B's destructor should be declared virtual if B have any other function declared virtual. B's functions should be declared virtual if you want other classes that inherit from B to be able to use them as virtual functions, just like with A->B

edit: don't know if my wording was that good. but if a function is virtual in A, but not in B, then it's B's version is the "final" one. ie. you can't make a new one in class C and except that to be called like ((A*)c)->MyFunc()

That's not a true at all. Once a function is declared virtual in the base A, it is ALWAYS virtual whether you explicitly write virtual in child classes or not. It's entirely up to you whether or not to write virtual in B or C. The outcome is exactly the same.

Share this post


Link to post
Share on other sites
As far as I know, once a function has been declared virtual in a class, it will also be virtual in the subclasses, regardless of the keyword virtual. (This is c++, right?)

Share this post


Link to post
Share on other sites
Quote:
Original post by Koen
As far as I know, once a function has been declared virtual in a class, it will also be virtual in the subclasses, regardless of the keyword virtual. (This is c++, right?)

correct

Share this post


Link to post
Share on other sites
Quote:
Original post by Koen
As far as I know, once a function has been declared virtual in a class, it will also be virtual in the subclasses, regardless of the keyword virtual. (This is c++, right?)


Yes, but it makes it alot easier to read if you write virtual in subclasses as well :)

Share this post


Link to post
Share on other sites
Quote:
Original post by stein
Yes, but it makes it alot easier to read if you write virtual in subclasses as well :)

I know (and I do write the virtual keyword, even if it's not necessary). I was only maken a statement about the syntax, not about how it should or shouldn't be used ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by Koen
Quote:
Original post by stein
Yes, but it makes it alot easier to read if you write virtual in subclasses as well :)

I know (and I do write the virtual keyword, even if it's not necessary). I was only maken a statement about the syntax, not about how it should or shouldn't be used ;)


Yea, I know... Didn't mean it like that - just wanted to point out the benefit I see in the keyword :)

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!