Advertisement

Virtual?

Started by January 19, 2003 05:42 PM
56 comments, last by John Dowis 21 years, 9 months ago
quote: Original post by ph33r
So making a function virtual "in case" has no real effect that I can tell.

A base class'' invariant behaviour, the behaviour which is part of the generalisation of its derived classes, should not be made up of virtual functions. When you make a function virtual you are specifically stating that the details of how the function fulfils its task may differ in derived classes. If this is not the case, then the function does not make sense if it is declared virtual. There should be no "in case".
quote: Original post by SabreMan
they say "well, someone might want to inherit from this class, so I''ll make the functions virtual"

One of Meyers'' points, IMO, applies to unnecessarily pondering whether you may need to derive from your class. "Make non-leaf classes abstract." That means that you should only ever instantiate objects from the most derived classes. All base classes should be abstract. So if you think you may need to derive from your class later than it''s destined to be a generalisation. It changed my perspective on a few things. But I''m unsure of how much people follow that rule and how important it is. I, personally, really like the theory behind it.
"C combines all the power of assembly language with all the ease of use of assembly language"
Yikes, this thread exploded overnight. I went to the bookstore last night and bought C++: The Complete Reference, fourth edition. Answered my question, but damn, LaMothe still uses some funny stuff in his code that not even that book has reference to. But, when I look at it long enough, I (usually) figure it out.

Thanks for all the help guys.

-John Dowis
Advertisement
Sabreman(or anyone else who knows),
You once told me this
quote:
The rule for destructors is more complicated than what you say, and here it is: if you plan to delete a derived class via a pointer to base, then the base class must have a virtual dtor. Memorize it now, worry about why later.


I feal confident now that I understand polymorphism but I would like to know the why now.

error: 500x1
quote: Original post by Whoknewb
Sabreman(or anyone else who knows),
You once told me this

quote: Original post by Sabreman
The rule for destructors is more complicated than what you say, and here it is: if you plan to delete a derived class via a pointer to base, then the base class must have a virtual dtor. Memorize it now, worry about why later.

quote: Original post by Whoknewb
I feal confident now that I understand polymorphism but I would like to know the why now.

In order to destroy a derived class object, the derived class destructor must be called, then the parent base class' destructor must be called, then the next base class' destructor must be called and so on up the inheritance hierachy. If you do not declare your base class destructors virtual, then the call is made statically. If you have a derived class object being treated as a base class object and delete it without the destructor being virtual, then the destructor which gets called will have been resolved at compile time as the base class destructor. This is not the destructor which you want called. By declaring the base class destructor as virtual, you allow the call to be resolved at runtime and everything will work correctly with the other destructors up the inheritance hierachy being called as required. I'm not clued up on the C++ standard, but Meyers says that deleting an object which you are treating polymorphically will result in undefined behaviour.


[edited by - NotAnAnonymousPoster on January 20, 2003 6:26:56 PM]
"C combines all the power of assembly language with all the ease of use of assembly language"
quote: Original post by NotAnAnonymousPoster
In order to destroy a derived class object

Aside: the term "class object" has a well established meaning in various languages which makes it''s usage in this context confusing. Just "object" will do.
quote:
If you have a derived class object being treated as a base class object and delete it without the destructor being virtual, then the destructor which gets called will have been resolved at compile time as the base class destructor.

That''s not what the C++ Standard specifies. All it says is that, if you delete a derived via a pointer to base, the resultant behaviour is undefined. A non-virtual dtor is then used as an obscure idiom for indicating a class was not designed with derivation in mind. However, it *is* OK (technically) to derive from a class with a non-virtual dtor, if it will not be deleted via a pointer to its base.
quote: Original post by SabreMan
Aside: the term "class object" has a well established meaning in various languages which makes it''s usage in this context confusing. Just "object" will do.

How do I specify that it''s instantiated from a derived class? How about "object from a derived class?"

"C combines all the power of assembly language with all the ease of use of assembly language"
Advertisement
quote: Original post by NotAnAnonymousPoster
How do I specify that it''s instantiated from a derived class? How about "object from a derived class?"

How about "instance of a derived class"?
That sounds much better.
"C combines all the power of assembly language with all the ease of use of assembly language"
quote:
All it says is that, if you delete a derived via a pointer to base, the resultant behaviour is undefined


Ok I can understand that but what if your base class in which it is a pure virtual class with no member variables such as

class Base
{
public:
Base();
~Base();
virtual void Create() = 0;
virtual void Destroy() = 0;
};

Now I''m not defining the base class'' dtor as virtual but I''m also don''t have any member variables to "clean up" after so do I really need a dtor? What is wrong with not making the dtor virtual?




quote: Original post by Whoknewb
Now I''m not defining the base class'' dtor as virtual but I''m also don''t have any member variables to "clean up" after so do I really need a dtor?

You *always* need a dtor. It is impossible not to have one.
quote:
What is wrong with not making the dtor virtual?

In the example you''ve provided, it looks like deleting a derived instance via pointer to base is likely. If that is the intention, then you should have a virtual dtor, else the behaviour will be undefined.

This topic is closed to new replies.

Advertisement