If we dont have virtual constructors then why we have virtual destructors? Can constructors also be virtual?
If we dont have virtual constructors then why we have virtual destructors? Can constructors
Share
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.
type is created, and it is well known in compile time. The compiler
do not need [and actually cannot, since the dynamic dispatch is based
on information which is created only after the object was created].
So there are no virtual constructors.
monitor the system. Assume you have
A* a = new B;[Binheritsfrom
A], and you laterdelete a;– the compiler has no way ofknowing
ais aB[in the general case], and will invokeA‘sdestructor – if it wasn’t virtual, and you might get a memory leak,
or other faults.
B‘s destructor isinvoked, since a
Bobject is being destroyed.