C++ Programming - Constructors and Destructors - Discussion
Discussion Forum : Constructors and Destructors - General Questions (Q.No. 3)
3.
Can a class have virtual destructor?
Discussion:
22 comments Page 1 of 3.
Tamal said:
2 years ago
Yes, a class can have a virtual destructor in C++.
When a class contains a virtual destructor, it means that the destructor can be overridden by a derived class. This is useful when we have a base class pointer pointing to a derived class object and we want to destroy the object through the base class pointer.
In this case, if the destructor is not virtual, only the base class destructor will be called, which may lead to memory leaks or undefined behaviour if the derived class has allocated some resources.
By making the destructor virtual, we can ensure that the destructor of the derived class is also called along with the base class destructor, which allows the derived class to release its resources before the object is destroyed.
Here's an example of a class with a virtual destructor:
class Base {
public:
virtual ~Base() {
// Virtual destructor
}
};
class Derived : public Base {
public:
~Derived() {
// Derived destructor
}
};
int main() {
Base* ptr = new Derived();
delete ptr; // Calls both Base and Derived destructors
return 0;
}
When a class contains a virtual destructor, it means that the destructor can be overridden by a derived class. This is useful when we have a base class pointer pointing to a derived class object and we want to destroy the object through the base class pointer.
In this case, if the destructor is not virtual, only the base class destructor will be called, which may lead to memory leaks or undefined behaviour if the derived class has allocated some resources.
By making the destructor virtual, we can ensure that the destructor of the derived class is also called along with the base class destructor, which allows the derived class to release its resources before the object is destroyed.
Here's an example of a class with a virtual destructor:
class Base {
public:
virtual ~Base() {
// Virtual destructor
}
};
class Derived : public Base {
public:
~Derived() {
// Derived destructor
}
};
int main() {
Base* ptr = new Derived();
delete ptr; // Calls both Base and Derived destructors
return 0;
}
(2)
Shikha Singh said:
8 years ago
@Arun
No, because it doesn't make any sense in C++.
Virtual functions are invoked when you have a pointer/reference to an instance of a class.
Static functions aren't tied to a particular instance, they're tied to a class.
C++ doesn't have pointers-to-class, so there is no scenario in which you could invoke a static function virtually.
No, because it doesn't make any sense in C++.
Virtual functions are invoked when you have a pointer/reference to an instance of a class.
Static functions aren't tied to a particular instance, they're tied to a class.
C++ doesn't have pointers-to-class, so there is no scenario in which you could invoke a static function virtually.
Swami vivekanand L. said:
1 decade ago
Destructor can be virtual because whenever the object goes in out of the scope then we have to delete it explicitly i.e. we need to call the destructor explicitly so destructor should made as virtual destructor like same as virtual function.
Amit said:
9 years ago
Virtual destructors are used to delete memory allocated for derived class. By default the destructor deletes memory just for base class.
Velmurugan said:
1 decade ago
Destructor is invisibility in your program and it's call automatically. Because every constructor program using inside destructor.
Dwarika said:
1 decade ago
Destructor can be virtual because whenever the object goes in out of the scope then we have to delete it explicitly.
Priyanka said:
1 decade ago
But if object goes out of scope, destructor is called automatically. Then why we need to call it explicitly?
Waseem Ahmad Naeem said:
1 decade ago
If an object goes out of scope destructor is called automatically then why we need to call it explicitly?
Nishant Singh said:
1 decade ago
Virtual functions provide run time polymorphism as the function to be executed is decided at run-time.
Lilly said:
1 decade ago
Please clarify my doubt. Class is declared as virtual then how the class contain destructor?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers