answersLogoWhite

0


Best Answer

If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object. There is a simple solution to this problem

User Avatar

Wiki User

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

Wiki User

6y ago

The term virtual constructor is often used (somewhat erroneously) when referring to an object factory. That is, a function or class which is used to instantiate objects of more than one type. However, there can never be a situation where we can construct an object of unknown type let alone construct a derived object knowing only its base class, thus there is no such thing as a virtual constructor.

We use a virtual function (also known as a method) to define a runtime interface; an interface to a method that may or may not be known at compile time but which must be known at runtime. Whenever we invoke that method, we expect the most-derived override of that method to be invoked, regardless of which (known) interface we use to invoke that method. This ensures objects behave according to their actual runtime type even when the runtime type cannot be determined at compile time (also known as runtime polymorphism).

During construction, the runtime type of the object being constructed has to be known at compile time because, as previously mentioned, we cannot construct an object of unknown type. After all, if we don't know runtime type of the object we wish to construct, what chance would the compiler have? If we must know the runtime type of the object we wish to construct, what benefit would a virtual constructor provide given there can be no construction override that is more derived than that of the object we wish to construct?

If we want an object of type X, then we must explicitly invoke an X::X constructor. If Y derives from X and we really wanted a Y then we must explicitly invoke a Y::Y constructor. There is no need for a virtual constructor because we always know the exact type of the object we wish to construct at compile time and what is known at compile time can always be statically bound; there is no need to use the virtual function call mechanism (dynamic binding).

Not only must we know the exact type of the object itself, we must also know the exact type of all its bases. That's important because in order to construct a Y which is derived from X, we must first construct an X. The compiler knows this because we already told it that Y derives from X; it's an integral part of the Y class definition. Moreover, when X has a choice of constructors available, we can explicitly instruct the compiler as to which specific X::X to invoke for any given Y::Y. Again, this is an integral part of the Y class definition; even if we do not specify which base class constructor to invoke, its default constructor is implied (if it doesn't have one then Y::Y is ill-formed).

Remember that construction is always bottom-up, starting with the least-derived class. The virtual function call mechanism is intended to provide runtime access to the most-derived override of a given method (regardless of which interface is used) but, during construction, the most-derived method does not exist until all the base classes of the class that defines it have been fully constructed. This is why we (generally) avoid invoking a virtual method from within a constructor. It makes even less sense for the constructor itself to be virtual because there can never be a more-derived constructor than the one that is currently executing!

Destruction, of course, is the complete opposite of construction. Destruction is top-down, destroying the most-derived class first. Thus, invoking a virtual method from within a destructor is best avoided because the most-derived override cannot be any more derived than the class that is currently being destroyed. If a more-derived override existed, it's no longer available!

This does not mean we cannot invoke virtual methods during construction or destruction, but we must be aware of the consequences of doing so. Remember that an object only exists from the point its constructor returns until the point its destructor is invoked. During construction or destruction the object being created or destroyed only exists insofar as its bases exist. At best, it is only a partial object.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

There is no such thing as a virtual constructor. Constructors are local to the class in which they are declared, and therefore cannot be inherited. Derived classes will call the default constructor of the least-derived base class first, unless a specific base class construction overload is called via the derived class's initialisation list. All base class constructors required by derived classes must be declared protected or public (private constructors are inaccessible), but no constructor can be declared virtual.

Destructors only need to be declared virtual when there are one or more virtual or pure-virtual methods declared in the class. Derived classes do not need to declare virtual destructors since it is implied by their base class. The only reason for having virtual destructors is to ensure the most-derived class destructor is called first whenever its base class falls from scope. Destruction then cascades up the hierarchy to the base class, which must be destroyed last.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

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.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are virtual constructors or destructors?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the Difference between virtual methods and non virtual methods in c sharp?

A function is a method that returns a value other than void. Methods includes functions, subroutines, constructors, destructors, and properties.


What types of functions cannot be made virtual?

Static member functions, member function templates and constructors cannot be virtual.


How can you create a virtual copy constructor?

You cannot. Constructors are specific to the class in which they are declared. They cannot be inherited and so they cannot be virtual.


What function initializes variables in a class?

you have to initialize it into the "Constructor" which is a function without dataType and it's name is the same with the class nameEX:-class Exforsys{private:int a,b;public:Exforsys();...};Exforsys :: Exforsys(){a=0;b=0;}the EX from : http://www.exforsys.com/tutorials/c-plus-plus/class-constructors-and-destructors-in-c.htmlI recommend this link for You Under the title "Constructors and destructors" :http://www.cplusplus.com/doc/tutorial/classes/You can also See : http://www.win.tue.nl/~maubach/university/education/online-references/cpp/swartz/notescpp/oop-condestructors/constructors.html


How can you justify the use of constructor and destructor in c plus plus?

Constructors allow class designers to initialise class attributes at the point of instantiation (whenever an object of the class is created). All classes must have at least one constructor other than the copy and move constructors otherwise it would be impossible to instantiate objects of the class. Copy and move constructors are not required but are generated automatically by the compiler unless the class designer explicitly marks them deleted from the class definition. The copy and move constructors allow new instances to be constructed from existing instances. The only difference between the two is that the move constructor transfers ownership of the member attributes, rather than merely copying them. In classes that contain pointers, the compiler-generated copy constructor only copies the pointer (a shallow copy) not what it points at (a deep copy). Thus class designers must provide a copy constructor in order to deep copy any unshared memory resources. Derived class constructors automatically invoke base class constructors, so classes are always constructed from the bottom up. Copy and move constructors automatically invoke their base class copy and move constructors, respectively, however all other constructors automatically invoke their base class default constructors, which may not be the most efficient method of construction. Thus class designers can invoke specific base class constructors through the constructor's own initialisation list (which is also used to initialise member attributes). Constructor bodies do not require any code except in those cases where initialisation is not possible with the initialisation list alone, however this is usually an indication of poor design choices rather than a limitation of the initialisation list. The initialisation list provides the most efficient mechanism for class initialisation. Every class must have one (and only one) destructor. If one is not provided by the class designer, the compiler generates one for you. However, unless the class contains a pointer to unshared memory, there is no need to define your own. Otherwise you must provide a destructor in order to release the memory. The destructor is the last chance to do so before an object of the class falls from scope. Classes that are intended to act as base classes must provide a virtual destructor (that is, the lowest base class must declared the destructor virtual). This ensures that if an object is destroyed polymorphically (via a pointer to one of its base classes), the most-derived destructor is always invoked first. Destruction of hierarchies is always top down. Note that destructors are not virtual by default for the simple reason that not all classes are intended to act as base classes. The containers provided by the Standard Template Library (STL) are a good example of this as none of the containers have virtual destructors -- so they cannot be used as base classes. Note that if any class method is declared virtual, the destructor must also be declared virtual. However, when inheriting from a base class with a virtual destructor, the class destructor is implicitly virtual (as are all virtual functions inherited from base classes) and does not need to be specified, although its good practice to explicitly include it anyway.

Related questions

What is the Difference between virtual methods and non virtual methods in c sharp?

A function is a method that returns a value other than void. Methods includes functions, subroutines, constructors, destructors, and properties.


What types of functions cannot be made virtual?

Static member functions, member function templates and constructors cannot be virtual.


Can you have virtual constructor?

The short answer is no, you cannot. The long answer is that constructors are not functions that can be called directly, and overriding a base class constructor would have no practical meaning since the derived class is itself responsible for calling its own base class constructors (whether implied by omission or explicitly via the derived class' initialisation list). Even so, the derived class isn't calling the base class constructor directly (that's why constructors have no return value; the actual call is made behind the scenes). The base class itself may be derived in which case its base class must be constructed before it can be constructed. This is the complete reverse of how a virtual function behaves, and is the reason that destructors can be virtual but constructors cannot. When a base class is destroyed, all its derivatives must be destroyed first, starting with the most-derived class of object. This can only be achieved through virtual destruction.


What are the various concepts of c plus plus?

1.Classes and Objects 2.Constructors and Destructors 3.Inheritance 4.Polymorphism 5.Dynamic Binding


How can you create a virtual copy constructor?

You cannot. Constructors are specific to the class in which they are declared. They cannot be inherited and so they cannot be virtual.


What are constructors and destructors and how they work?

Constructors & destructors are special member fns of a class. Constructor is a fn with the same name of the class and is used to initialize the object. ie automatic initialization is done by constructor. When an object is invoked then constuctor should be called. Similarly destructor is also a speial function is used to destroy the objects. Its name is also same as class name, but a tild(~) symbol should be used along with destruction function name. eg:~classname()


What do you mean by finalizer method?

In C++ you have object constructors and object destructors. Both are called by the developer. In Java and C# you have constructors and finalizer methods, so Java and C# both have support for finalizer methods (also known simply as finalizer). So the finalizer methods are similar to the destructors of C++ with a very important twist, the finalizer method is called by the garbage collector when an object is freed and not by the programmer (like the destructors in C++). Both finalizers in Java, C# and destructors in C++ can be used to free resources such as sockets or file handles that the method is using. However, because the finalizer methods are called by the garbage collector the programmer has no control of when the finalizer method will be called. As such it is NOT A GOOD IDEA to use finalizer methods. One can write the methods of an object in such a way as to clean up after themselves.


What function initializes variables in a class?

you have to initialize it into the "Constructor" which is a function without dataType and it's name is the same with the class nameEX:-class Exforsys{private:int a,b;public:Exforsys();...};Exforsys :: Exforsys(){a=0;b=0;}the EX from : http://www.exforsys.com/tutorials/c-plus-plus/class-constructors-and-destructors-in-c.htmlI recommend this link for You Under the title "Constructors and destructors" :http://www.cplusplus.com/doc/tutorial/classes/You can also See : http://www.win.tue.nl/~maubach/university/education/online-references/cpp/swartz/notescpp/oop-condestructors/constructors.html


How can you justify the use of constructor and destructor in c plus plus?

Constructors allow class designers to initialise class attributes at the point of instantiation (whenever an object of the class is created). All classes must have at least one constructor other than the copy and move constructors otherwise it would be impossible to instantiate objects of the class. Copy and move constructors are not required but are generated automatically by the compiler unless the class designer explicitly marks them deleted from the class definition. The copy and move constructors allow new instances to be constructed from existing instances. The only difference between the two is that the move constructor transfers ownership of the member attributes, rather than merely copying them. In classes that contain pointers, the compiler-generated copy constructor only copies the pointer (a shallow copy) not what it points at (a deep copy). Thus class designers must provide a copy constructor in order to deep copy any unshared memory resources. Derived class constructors automatically invoke base class constructors, so classes are always constructed from the bottom up. Copy and move constructors automatically invoke their base class copy and move constructors, respectively, however all other constructors automatically invoke their base class default constructors, which may not be the most efficient method of construction. Thus class designers can invoke specific base class constructors through the constructor's own initialisation list (which is also used to initialise member attributes). Constructor bodies do not require any code except in those cases where initialisation is not possible with the initialisation list alone, however this is usually an indication of poor design choices rather than a limitation of the initialisation list. The initialisation list provides the most efficient mechanism for class initialisation. Every class must have one (and only one) destructor. If one is not provided by the class designer, the compiler generates one for you. However, unless the class contains a pointer to unshared memory, there is no need to define your own. Otherwise you must provide a destructor in order to release the memory. The destructor is the last chance to do so before an object of the class falls from scope. Classes that are intended to act as base classes must provide a virtual destructor (that is, the lowest base class must declared the destructor virtual). This ensures that if an object is destroyed polymorphically (via a pointer to one of its base classes), the most-derived destructor is always invoked first. Destruction of hierarchies is always top down. Note that destructors are not virtual by default for the simple reason that not all classes are intended to act as base classes. The containers provided by the Standard Template Library (STL) are a good example of this as none of the containers have virtual destructors -- so they cannot be used as base classes. Note that if any class method is declared virtual, the destructor must also be declared virtual. However, when inheriting from a base class with a virtual destructor, the class destructor is implicitly virtual (as are all virtual functions inherited from base classes) and does not need to be specified, although its good practice to explicitly include it anyway.


When was The Destructors - band - created?

The Destructors - band - was created in 1977.


Does c language really supports destructors?

it doesnt support destructors


How do you describe the importance of destructors?

Destructors are used to free memory and release resources.