Parent class has a Virtual function. Is it necessary to have a virtual destructor in the parent class ?
So the questions are
1. Now considering rule of three , should we declare the other two?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A virtual destructor is required any time
deleteis called on a pointer to that class where the object being deleted is actually of a more derived type. If your base class may be used in this sort of situation then you must provide a virtual destructor.It is usually advisable to add a virtual destructor to any class that has virtual functions as this gives safety and flexibility to future users of the class and the additional cost of adding a virtual destructor to an already polymorphic class is typically low.
This last rule is just a rule of thumb, though. It may be necessary to have a virtual destructor in a class even if it doesn’t have any other virtual functions and, conversely, a virtual destructor may not be needed for a polymorphic class if it is never used in a context that requires this (typically the destructor would be made
protectedor evenprivateto enforce this).You only need to provide a user-defined copy constructor and a copy assignment operator if the compiler provided default implementations won’t do the correct thing. If you’ve added a destructor with an empty implementation purely to make it virtual, this is unlikely to have any bearing on the need for you to provide a copy constructor and a copy assignment operator.