When do you need a destructor to be virtual? I read that a destructor should be virtually if it is an abstract class. |
Any time you expect to delete a derived class instance through a base class pointer. Since abstract classes are intended to be used in a way which pretty much guarantees you'll be doing this, it makes sense for abstract classes to have virtual destructors.
So if a class is not the base class, do I ever set the destructor of a derived class to be virtual? |
If a class is not a base class it is never derived from and the situation described above never occurs.
Aren't all virtual declarations inherited from the base class so everything that is derived from it becomes virtual if the same named thing was declared it in the base class? |
Yes and no. Functions which override virtual functions are virtual. Functions with the same name as a virtual function but which do not actually override the function (instead providing an overload with a different signature) are not virtual (and hide the virtual function in the class's static interface.)
So why is it that some derived classes have virtual destructors? If is just bad coding or does it become a pseudo base class if other classes are derived off the original derived class?
|
If the base class has a virtual destructor it is redundant, but may serve to document the fact that the class is intended to be used polymorphically.
If there is a base class without a pure virtual function, does adding the = 0 to make it so change anything significant? |
Yes. Classes with pure virtual functions cannot be instantiated directly.
Do base structs need virtual destructors if it has a virtual function? |
structs should be treated the same as classes with regard to virtual destructors.
Please don't do what you did here:
http://www.cplusplus.com/forum/beginner/198999/
in future posts. Deleting posts is rude to those who answer you and those who search forums hoping to find questions/answers that may help them.