answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

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

Wiki User

12y ago

Constructors allow initialize classes with some initial data (not necessary relevant). It's done with purpose to prevent calling classes which was not initialized. Such call will cause not reversible error and in some case can cause your OS to freeze.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Destructors are called when an object falls from scope, or by deleting a pointer to an unreferenced object (references can never be NULL so don't delete them via pointers -- they will destroy themselves when they fall from scope).

If the object is derived, the call cascades up the inheritance hierarchy to destroy the underlying classes in the reverse order they were created, from the most-derived (your object) to the least-derived (the base class(es)).

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

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.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When destructor can be virtual in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


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.


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.


Constructor cannot be virtual but destructor can be virtual justify?

bcoz constructor cant be invoked

Related questions

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.


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.


What is a distructor in c plus plus?

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


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.


Constructor cannot be virtual but destructor can be virtual justify?

bcoz constructor cant be invoked


Can you overload destructor for your class?

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.


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.


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.


Why constructers can not be inherrited in c plus plus?

Every class of object must provide its own specific construction code. Since they are not really functions they cannot be overridden, thus they cannot be inherited. The same applies to the class destructor and the class self-assignment operator. Each class must handle these explicitly, within the class implementation. They cannot be implemented outside of the class, and therefore cannot be inherited. Although the virtual keyword can be applied to the destructor, it's not a function and therefore not a virtual function. The only reason for having a virtual destructor at all is when one or more of the class methods are virtual. Since classes with virtual methods are intended to act as generic interfaces for more specialised implementations, it's important that when destroying such a class that the most-derived (or most-specialised) class is destroyed first, working up the hierarchy to the base class which must always be destroyed last. This can only be achieved through virtual destruction. Failure to declare the base class destructor virtual could result in a base class being destroyed (such as when deleting a pointer to it), which would then result in an incomplete and therefore invalid derived class remaining in memory.


Does the C plus plus programming language use a virtual machine?

No, it does not. But Microsoft Visual Studio 2008 allows you to connect to a virtual machine and run your projects "sandboxed".