answersLogoWhite

0


Best Answer

No. Classes can only have one destructor, whether you define one yourself or allow the compiler to generate one for you. The compiler-generated destructor is public by default, does not release any memory allocated to any class' member pointers, and is non-virtual, which are the three main reasons for defining your own.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

No. If a destructor is declared it must be implemented by that class that declared it, whether the class is abstract or not. The destructor should be declared virtual (non-pure) if the class has the potential to be a base class, which is the case if there are one or more virtual methods besides the destructor. A virtual destructor simply ensures the most-derived destructor is called first, whenever a base class instance is explicitly destroyed.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

No, destructors cannot be overloaded. Standard methods as well as constructors may be overloaded.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

No. But you can override it. A default destructor is provided even if you don't declare one.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you overload destructor for your class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between destructors in c plus plus and c sharp?

In C# only class instances can have a destructor, whereas both class and struct instances can have a destructor in C++. While syntactically similar, a C++ destructor executes exactly as written, whereas a C# destructor merely provides the body of the try clause of the class' finalize method.


What is the order of construction and destruction in c plus plus?

The least-derived base classes are always constructed first, in the order specified by the derived class inheritance list. The most-derived class (the one you are actually instantiating) is always constructed last. Destruction is basically the reverse of construction. However, base class destructors must be declared virtual to ensure that the most-derived class destructor is called first, regardless of which class destructor is actually invoked. That is, if you hold a pointer to a base class that has no virtual destructor, deleting that pointer will only destroy the base class, not the derived class, leaving the derived class in an invalid state (because it no longer has an underlying base class) and with no way to recover the memory it consumes. It is important to remember that if you declare any virtual methods within a class, you must also declare the destructor virtual. A class without a virtual destructor is not intended to be derived from. If it has virtual methods, but no virtual destructor, it is not well-formed and must not be used as a base class.


A method that is automatically called when an instance of a class is created?

The constructor of a class is automatically called when an instance of the class is created (using new in C++). The constructor method has the same name as the class that it is a part of. Constructors have no type and do not return anything. Similarly, the destructor is automatically called when the instance of the class is destroyed. The destructor is the same name as the class and is preceded by a tilde (~) For example: class Example { public: Example() // Constructor { printf("Object created\n"); } ~Example() // Destructor { printf("Object destroyed\n") } }; int main() { Example* x = new Example(); // Creates object, calls constructor delete x; // Calls destructor, deletes object return 0; }


When destructor can be virtual in c plus plus?

A class constructor initialises object member variables, ensuring the object is in a valid state from the point of instantiation. The destructor resets those same variables when the object falls from scope. Typically, the destructor is only required to release any memory allocated to any internal class pointers.


How do you write a C plus plus program for destructor?

Class destructors define operations that will be performed whenever an object of the class falls from scope. This usually involves manually releasing any resources allocated to the object. However, by using resource handles or smart pointers, all resources will be released automatically, thus there is no need to define a destructor. The only time we really need to define a destructor is when the class is intended to be used as a polymorphic base class (has one or more virtual methods) but does not inherit a virtual destructor, in which case we must define a virtual destructor. Classes that do define or inherit a virtual destructor cannot be used polymorphically. However, by using resource handles or smart pointers to manage resources, the destructor body can be left empty. The only reason for declaring the destructor at all is simply to declare it virtual because all methods are non-virtual by default -- unless they override a virtual method of the base class (in which case they can simply be declared as overrides). The only time we need to define a non-empty destructor body is when implementing a resource handle or smart pointers, however the standard library already provides efficient implementations so, other than for educational purposes, there is no need to define our own resource handles.

Related questions

What is the difference between destructors in c plus plus and c sharp?

In C# only class instances can have a destructor, whereas both class and struct instances can have a destructor in C++. While syntactically similar, a C++ destructor executes exactly as written, whereas a C# destructor merely provides the body of the try clause of the class' finalize method.


What is the order of construction and destruction in c plus plus?

The least-derived base classes are always constructed first, in the order specified by the derived class inheritance list. The most-derived class (the one you are actually instantiating) is always constructed last. Destruction is basically the reverse of construction. However, base class destructors must be declared virtual to ensure that the most-derived class destructor is called first, regardless of which class destructor is actually invoked. That is, if you hold a pointer to a base class that has no virtual destructor, deleting that pointer will only destroy the base class, not the derived class, leaving the derived class in an invalid state (because it no longer has an underlying base class) and with no way to recover the memory it consumes. It is important to remember that if you declare any virtual methods within a class, you must also declare the destructor virtual. A class without a virtual destructor is not intended to be derived from. If it has virtual methods, but no virtual destructor, it is not well-formed and must not be used as a base class.


A method that is automatically called when an instance of a class is created?

The constructor of a class is automatically called when an instance of the class is created (using new in C++). The constructor method has the same name as the class that it is a part of. Constructors have no type and do not return anything. Similarly, the destructor is automatically called when the instance of the class is destroyed. The destructor is the same name as the class and is preceded by a tilde (~) For example: class Example { public: Example() // Constructor { printf("Object created\n"); } ~Example() // Destructor { printf("Object destroyed\n") } }; int main() { Example* x = new Example(); // Creates object, calls constructor delete x; // Calls destructor, deletes object return 0; }


When destructor can be virtual in c plus plus?

A class constructor initialises object member variables, ensuring the object is in a valid state from the point of instantiation. The destructor resets those same variables when the object falls from scope. Typically, the destructor is only required to release any memory allocated to any internal class pointers.


What is a distructor in c plus plus?

A destructor destroys an instance of a class to free up memory.


Why constructor and destructor cannot be made static?

The term "destructor" made me believe this question is related to .Net languages. A destructor is to destroy an instance of object. If it is available at static/class level, what is it going to destroy? The entire class, so the class no longer available? Thus, semantically, destructor should be an instance method. Constructor is on the opposite end of the life cycle of an instance. However, in .NET, a static constructor is allowed. Personally, I call this static constructor as a class initialization method. This method will be invoked by the .net framework only once when the class is loaded into the application domain. With the similar concept, there should be a "finalizer" of the class when the class is unloaded out of the application domain. But wait, does a class ever go out of the application domain once it's loaded? Yes, only at the termination of the application! Currently a class cannot be unloaded explicitly in codes and thus no point to have a static finalizer.


How can you recognize a constructor in a class?

A class's constructor will have the same name of the class and no return type (not even void): class Example(){ Example() {printf("This is the constructor\n");} ~Example(){printf("This is the destructor\n");} };


How do you write a C plus plus program for destructor?

Class destructors define operations that will be performed whenever an object of the class falls from scope. This usually involves manually releasing any resources allocated to the object. However, by using resource handles or smart pointers, all resources will be released automatically, thus there is no need to define a destructor. The only time we really need to define a destructor is when the class is intended to be used as a polymorphic base class (has one or more virtual methods) but does not inherit a virtual destructor, in which case we must define a virtual destructor. Classes that do define or inherit a virtual destructor cannot be used polymorphically. However, by using resource handles or smart pointers to manage resources, the destructor body can be left empty. The only reason for declaring the destructor at all is simply to declare it virtual because all methods are non-virtual by default -- unless they override a virtual method of the base class (in which case they can simply be declared as overrides). The only time we need to define a non-empty destructor body is when implementing a resource handle or smart pointers, however the standard library already provides efficient implementations so, other than for educational purposes, there is no need to define our own resource handles.


Why is a destructor function required in C plus plus classes?

A destructor is not a function per-se. Functions have returns values but destructors do not (not even void). However, other than the lack of a return value, a destructor's body is no different to a function body. Contrary to belief, destructors are not required by every class of object. Destructors are only required when we need to explicitly release a resource when an object of the class falls from scope, but not all resources need to be released explicitly. Consider the following class: struct point { int x, y; }; This class does not require a destructor because it is self-contained. That is, all the resources consumed by an object of the class are contained within the object itself, thus when the object falls from scope, the memory it consumed is automatically released. There is nothing to destroy. Now consider the following class: class my_vector { std::vector<int> v; }; Again, this class requires no destructor. When objects of this class fall from scope, the vector's destructor is invoked, releasing its resources. Thus the only resource consumed by this class is the vector itself, and that's self-contained by the class. Now let's consider a class that does require a destructor: template<typename T> class smart_pointer { T* ptr; // ... }; Here, we have a class that encapsulates a "naked" pointer variable of some type T. That pointer could feasibly refer to any object of type T in memory. However, being a "smart pointer", the assumption is that objects of this class will "own" the memory they refer to. As the owner, it makes sense to yield ownership whenever object's of the class fall from scope. If we didn't provide a destructor, ptr would fall from scope, but the memory it referred to would not. So to ensure the memory is released, we must explicitly release it via the class destructor: template<typename T> class smart_pointer { T* ptr; // ... public: ~smart_pointer () { delete ptr; } }; To summarise: a destructor is only required when an object acquires a non-shared resource that is not released automatically when objects of the class fall from scope. A vector is a resource handle, so when it falls from scope, its resources are released automatically. Similarly with smart pointers. However a "naked" pointer is not a resource handle, so if our class "owns" a resource by holding a pointer to it, then we must release that pointer when objects of the class are destroyed. Resources cover many things besides raw memory. If we hold a reference to an open file, it would be prudent to close the file before the reference falls from scope. With a resource handle we get that assurance. Thus if we use resource handles rather than "raw" resources, then we need never write any destructors. There can be other reasons to require a destructor besides resource management, such as to write an entry in a log file upon destruction, however resource management and other "housekeeping" task are not a requirement of every class thus a destructor is not required by every class.


When was Varroa destructor created?

Varroa destructor was created in 2000.


What are the characteristics of a destructor?

The main characteristics of a Destructor are:Are inverse of ConstructorsCalled when objects are destroyedUsed to unlink an object resource / clean up resources used by an instance of a classAutomatically called when the instance of the class goes out of scopeDoesn't return any valueHas the same name as that of the class with a special character infront ( like ~ in C++ )


How do you implement inheritance in c plus plus?

You implement inheritance by deriving a new class of object from an existing class of object. The existing class is known as the base class of the derived class.Classes declared final cannot be used as bases classes and classes without a virtual destructor (or a virtual destructor override) cannot be used as polymorphic base classes.