answersLogoWhite

0

C is not an object-oriented language -- there are no destructors.

In C++, however, an object's destructor is invoked automatically when the object falls from scope. The destructor can also be invoked by manually deleting a raw pointer to the object (or one of its base classes), however you should only ever use the delete operator if the object was instantiated with the new operator, and only after all references or pointers to the object have fallen from scope. The safest way to manage raw pointers is to use a resource handle or smart pointer.

User Avatar

Wiki User

10y ago

What else can I help you with?

Continue Learning about Engineering

Constructor cannot be virtual but destructor can be virtual justify?

bcoz constructor cant be invoked


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.


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 does the delete operator do in addition to deallocation of memory space?

The delete operator calls the destructor of the object referenced by its operand. If the destructor is virtual, the destructor of each superclass up to the top of the inheritance hierarchy is also called, in order. If you don't define a destructor for a class, the compiler defines a default destructor that has no effect. Fundamental types (char, int, float, etc.) do not have destructors, so using delete has no other effects.As an aside: when you use inheritance, make sure to make your destructors virtual, so that objects are properly destroyed!Also note that you should not use C's free() on a pointer that you got from C++'s new, or use C++'s delete on a pointer you got from C's malloc(). These are not guaranteed to work, and mixing them might cause Big Bad Things to happen. In general, there is no reason to use malloc()/free() in C++ at all.


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.

Related Questions

Constructor cannot be virtual but destructor can be virtual justify?

bcoz constructor cant be invoked


Why pointer of constructor is made but not of destructor?

When a constructor is invoked dynamically, the new operator allocates the required memory, initialises it according to the constructor, then returns a pointer to the allocation. The destructor is invoked by deleting the pointer. It wouldn't make any sense to return a pointer from a deletion.


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.


Constructor and destructor invocation in c?

Not possible in C.


Which is the function which destroys objects in C?

No objects in C. For C++, it is destructor.


Is destructor allowed in java?

No. Java does not support the concept of Destructors like C


Why destructure not have any return type?

I assume you mean "destructor". A destructor is invoked automatically, when an object is destroyed. It is not invoked as a method, as in: x = anObject.destroy(); The above is NOT the way a destructor is invoked; you have no control over the destruction process, can't assign a possible return value to any variable, so it makes no sense to HAVE a return value. Destructors (and constructors for that matter) are not implemented as functions. They are simply procedures and procedures have no return value, not even void. This makes sense because when an object reference falls from scope, its destructor must be called automatically. And in the case of derived objects, the virtual destructors must be called in sequence, from most-derived to least derived. Returning temporary values that would simply be destroyed anyway is somewhat pointless when all you want to do is invoke the destructor itself. Even if an object were instantiated dynamically (with the new() operator), and you subsequently deleted a pointer to one of the object's base classes, virtual destruction ensures the cascade of destructors happens in the correct sequence. But the delete() operator that invoked the destruction itself returns void. Again, returning values from destructors that would simply be temporary and therefore automatically fall from scope would make no sense. Ultimately, no return value makes any sense in a destructor (or a constructor, for that matter). Even a return value of void would make no sense.


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.


How can a constructor be invoked at the time of inheritance in C Plus Plus?

It cannot. Inheritance is a compile-time operation. Constructors are invoked at runtime at the point of instantiation.


What does the delete operator do in addition to deallocation of memory space?

The delete operator calls the destructor of the object referenced by its operand. If the destructor is virtual, the destructor of each superclass up to the top of the inheritance hierarchy is also called, in order. If you don't define a destructor for a class, the compiler defines a default destructor that has no effect. Fundamental types (char, int, float, etc.) do not have destructors, so using delete has no other effects.As an aside: when you use inheritance, make sure to make your destructors virtual, so that objects are properly destroyed!Also note that you should not use C's free() on a pointer that you got from C++'s new, or use C++'s delete on a pointer you got from C's malloc(). These are not guaranteed to work, and mixing them might cause Big Bad Things to happen. In general, there is no reason to use malloc()/free() in C++ at all.


When was Varroa destructor created?

Varroa destructor was created in 2000.