answersLogoWhite

0


Best Answer

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.

##

Constructor called implicitly not explicitly so constructor is not virtual.

User Avatar

Wiki User

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

Wiki User

14y ago

We can't override a constructor. When any method is declared as virtual it should be overridden in its derived class. Since constructor is used for initialising the variables and if declare a constructor as virtual it cant be overridden

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

No, a virtual constructor would not only be meaningless, it would be ambiguous. The implication is that calling a base class constructor would somehow call a derived class constructor -- but how is the compiler to determine which derived class it is meant to construct?

Derived objects can only be constructed by calling the derived object's constructor directly. In so doing, all base class constructors will be called in sequence, from the least-derived class upwards. Only when all the base classes are fully instantiated can the derived object itself be instantiated.

If you need to call specific base class constructors in order to pass parameters to them, you can do so via the derived object's initialisation list(s).

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The constructor must be non virtual because, at the time the constructor is fired, the virtual-table does not exist, and polymorphism is not supported yet.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can a constructor be declared as virtual?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


Constructor cannot be virtual but destructor can be virtual justify?

bcoz constructor cant be invoked


How constructor is different from normal member function?

A constructor differs from a normal member function in three ways:A constructor never returns a result. The constructor's declaration reflects this by not even declaring the function as "void." The common design hypothesis is that a well-designed constructor cannot fail, other than maybe in an irrecoverable way (such as a fatal running out of memory).A constructor is never called explicitly except with the new operator.Constructors impose further restrictions. For example, they cannot be declared abstract or virtual, and may have visibility requirements. The common design practise is that at least the default constructor is declared public.


1 Explain the concepts of constructor and destructor Do you have to declare a constructor every time you create a class?

You only need a constructor if the default constructor will not suffice. Often times, it is useful to have a constructor that takes common parameters so that you do not have to write additional code. For example, a Point class might have a constructor for Point(int x, int y), which would be a shortcut for assigning x and y independently. Other classes may not need any default values assigned, and for this, it is acceptable to just use the default constructor. Finally, some classes are virtual, static, or abstract, and so may not need a constructor because the constructor is unnecessary (static), or may be defined elsewhere (virtual, abstract).


What is friend constructor?

A friend constructor is a constructor that is declared a friend of another class and that grants that constructor private access to the class in which it is declared a friend. Example: class Y { friend char* X::foo (int); // friend function friend X::X (char); // constructors can be friends friend X::~X(); // destructors can be friends }; For more information, see '11.3 Friends' in the current ISO C++ Standard.

Related questions

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.


Constructor cannot be virtual but destructor can be virtual justify?

bcoz constructor cant be invoked


What happens when no constructor function is declared in a class - in Java?

When any constructor is deffined in your class, the java compiler create a default no argument constructor for you. This constructor only have an invocation to the super class constructor (" super( ) ").


How constructor is different from normal member function?

A constructor differs from a normal member function in three ways:A constructor never returns a result. The constructor's declaration reflects this by not even declaring the function as "void." The common design hypothesis is that a well-designed constructor cannot fail, other than maybe in an irrecoverable way (such as a fatal running out of memory).A constructor is never called explicitly except with the new operator.Constructors impose further restrictions. For example, they cannot be declared abstract or virtual, and may have visibility requirements. The common design practise is that at least the default constructor is declared public.


Can constructor be declared as constant in c plus plus?

No. Constructors initialise objects and, by definition, must be able to modify the member variables. Uninitialised members are a disaster waiting to happen even without a constructor declared const! Thankfully, the compiler won't permit a const constructor.


What is a dynamic constructor?

dynamic constructor is a way to constructing an object based on the run type of some existing object. it basically uses standard virtual functions/polymorphism


1 Explain the concepts of constructor and destructor Do you have to declare a constructor every time you create a class?

You only need a constructor if the default constructor will not suffice. Often times, it is useful to have a constructor that takes common parameters so that you do not have to write additional code. For example, a Point class might have a constructor for Point(int x, int y), which would be a shortcut for assigning x and y independently. Other classes may not need any default values assigned, and for this, it is acceptable to just use the default constructor. Finally, some classes are virtual, static, or abstract, and so may not need a constructor because the constructor is unnecessary (static), or may be defined elsewhere (virtual, abstract).


What is friend constructor?

A friend constructor is a constructor that is declared a friend of another class and that grants that constructor private access to the class in which it is declared a friend. Example: class Y { friend char* X::foo (int); // friend function friend X::X (char); // constructors can be friends friend X::~X(); // destructors can be friends }; For more information, see '11.3 Friends' in the current ISO C++ Standard.


What is the name of the special member function which is automatically called during creation of each class object?

The constructor. The constructor instantiates the object, and can optionally take parameters and has an optional initialization phase. It has no return type, and has the same name as the class itself. The constructor can be overloaded. It cannot be virtual or constant.


Can you define a constructor as private?

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


Can you declare and define the constructor within class?

Yes, you can declare and define the constructor within a class. A constructor is a special member function of a class that is automatically called when an object of the class is created. It is used to initialize the object's data members. The constructor can be declared and defined within the class definition or can be defined outside the class definition using the scope resolution operator (::).


What are virtual constructors or destructors?

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