Computer Programming
Java Programming
C Sharp

Can you declare a Destructor as Private?


Top Answer
User Avatar
Wiki User
2006-02-22 18:29:12
2006-02-22 18:29:12

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 ???


Related Questions

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.

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.

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

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.

To declare a class, a basic layout could look like this: //any header files that are going to be required in the class should //be defined here, for example //example inclusion of header file #include "name_of_header.hpp" class NameOfClass { public: //constructor NameOfClass(); //destructor ~NameOfClass(); private: //you can include any data members only accessible by the //class here };

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

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.

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.

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.

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.

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.

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

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.

Constructor creates an instance of class, destructor destroys it.

The cast of El destructor - 1985 includes: Eduardo Palomo

* 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.

I dont think we can have Protected Constructors but yes we can have Private constructors. We can declare the constructor as Private to ensure that no other class can instantiate it. We use this in the singleton design pattern

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.

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....

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).

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.

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.

Plz tel me disadvantages of destructor in c++

Destructor is a noun - a furnace for disposal of refuse. In this example it is the act or process of intentional destruction; The verb form would be to destroy or to be destroyed

A virtual function in C++ is a function that can be invoked through a pointer to some other base class, i.e. a different signature. A virtual destructor, then, is a destructor that is invoked through a pointer to a base class with a different signature. This is important for polymorphism. If you have a base class A, and a derived class B as a refinement of A, then both destructors will be called if you have an instance of B. However, if you have a pointer to A, but it points to an instance of B, B's destructor will not be invoked on the delete operation unless B's destructor is virtual.

Copyright ยฉ 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.