Best Answer

No .... Whats the point of declaring destructor as a private when it is supposed to be designed to free the memory after executing a program or a method ???

User Avatar

Wiki User

โˆ™ 2006-02-22 18:29:12
This answer is:
User Avatar
Study guides
See all Study Guides
Create a Study Guide

Add your answer:

Earn +20 pts
Q: Can you declare a Destructor as Private?
Write your answer...
Related questions

What is pure virtual destructor?

There is no such thing as a pure-virtual destructor. Classes can declare destructors to be virtual to ensure base class destructors call their derived class destructors first, but they cannot be declared pure virtual. This would imply the class is abstract and that the destructor must be overridden by all derived classes. But class destructors cannot be overridden. Every class, including an abstract class, must handle its own destruction. The only reason to declare a virtual destructor (but not a pure-virtual destructor) is when the class has one or more virtual methods besides the destructor.

Why are you virtual destructor in c plus plus?

You should declare a virtual destructor in any class that has one or more virtual methods besides the destructor. Such classes are intended to act as base classes, and this ensures the most-derived destructor is always called first, whenever an instance of a base class is explicitly destroyed.

Can you overload a destructer in c plus plus?

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

What are various advantage of constructor and destructor?

The only purpose of a constructor is to initialise an instance of your class in a controlled manner. The compiler generated constructors are not guaranteed to perform a controlled initialisation, particularly if your class allocates dynamic memory to its member variables, thus it is in your best interest to declare your own default and copy constructors to ensure correct initialisation. moreover, the default constructor access is public, so if you want to override this you must declare your own constructors. There are no disadvantages in declaring your own constructor. The destructor gives your class one final chance to clean up any dynamic memory allocated to its members. Again, it is in your interest to declare your own destructor but, again, there are no disadvantages in declaring your own destructor.

When was Varroa destructor created?

Varroa destructor was created in 2000.

What is issue in throwing an exception in a destructor?

Throwing an exception from within a destructor is a serious design flaw because abnormal termination of the destructor would result in a resource leak. At best, the object is only partially destroyed. If a destructor must throw an exception, the destructor must also catch the exception and handle it accordingly.

What is l drago destructor ultimate move?

Dragon Emperor Life Destructor.

Can destructor be declared as pure virtual?

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.

When destructor can be virtual in c plus plus?

A destructor should be declared virtual when the class is intended to be used as a base class. Not all classes are intended as base classes thus destructors are non-virtual by default. Derived classes do not need to declare virtual destructors as they can be declared with override (C++11). The reason base class destructors are declared virtual is to ensure that the class hierarchy is destroyed in the reverse order it was constructed, starting with the most-derived class destructor (regardless of which object in the hierarchy falls from scope first). If we don't declare the destructor virtual, we cannot guarantee the most-derived class will be destroyed first, which could leave a partial object in memory and thus create a resource leak.

Can private class have only private members?

The keyword private for the class is meant to be private to the class declare it. The private for the member is for the class contains that definition. So the answer is a private class may have public, protected and private members.

When is a destructor called in c plus plus?

Destructor usually called when programs exists. You can call destructor during execution of your program to free memory. If destructor is not called you get what is called memory leak. And if you operate with large volume of data in no time you can run out of memory.

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.

What is the difference between the constructor to and destructor?

Constructor is called when the object is created. Destructor is called when the object is destroyed

What will happen when the class is declared as private in java?

Hi, you can not declare your class as private except that is inner class.

What actors and actresses appeared in El destructor - 1985?

The cast of El destructor - 1985 includes: Eduardo Palomo

Why use a virtual destructor C plus plus?

It is used in combination with pointers that point to a base class. When the pointer points to a base class, but is assigned to derived object, then a destructor called by the pointer needs to be the correct one. It needs to call the destructor of the derived object, so in this case, the base class destructor needs to be virtual to let the compiler know to call the destructor of the derived object.

What is the opposite of inventor?


What does instructor rhyme with?


List out differennce between constructor and destructor in c plus plus?

Constructor creates an instance of class, destructor destroys it.

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 you not declare class as private or public in java?

whenever we declare a private class only the members of class can access that function or that class , when ever we declare a class as public it can be access from any where , so it give challenges to security in java .. So we can use protected specifier so that the member of that class and the sub class can use that class....

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 Destructor in c plus plus?

In a class, the Destructor is like the opposite of the Constructor. The Constructor is run when an object of the class is created (usually to set up the variables as needed), and the Destructor is run when it is destroyed (usually to clean up or delete variables).

What happens if an exception is throws from an object's destructor?

If a destructor throws an exception, the instance is left in an invalid state. When an exception is thrown, the destructor automatically terminates at the point of the throw, unwinding the call stack until an exception handler is found (if one is provided). However, any resources yet to be released by the destructor, including all the instance's base classes, cannot be destroyed. When writing your own destructors, it is important to never throw an exception. If an exception could be thrown from within your destructor, you must catch it and handle it within the same destructor -- you must not rethrow the exception.

What is the difference between constructor and destructor?

* A constructor is called when you want to create a new instance of a class. A destructor is called when you want to free up the memory of an object (when you delete it). * A constructor constructs the value of an object . A destructor destructs the value created by the constructor for the object. * Another differentiation is their syntax's : constructor : (Arguments){//body of constructor.} destructor : ~() { }The basic difference between constructor and destructor is constructor is called during the beginning of the program whereas the destructor is called at the end of the program.The former allocates memory whereas the later removes the memory.The constructor can be overloaded however the destructor cannot be overloaded.