to virtual or not to virtual

Started by
9 comments, last by Jolle 19 years, 5 months ago
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|
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()
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?
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.
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?)
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
great, thanks for answering my question. :)
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 :)
| Stein Nygård - http://steinware.dk |
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 ;)
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 :)
| Stein Nygård - http://steinware.dk |

This topic is closed to new replies.

Advertisement