473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

why we cannot create virtual constructor

why we cannot create virtual constructor.
Apr 15 '07 #1
4 2618
weaknessforcats
9,208 Expert Mod 8TB
The simple answer is that a virtual function is used to avoid a situation where the compiler has a choice between calling the base class function or the derived class function. The virtual keyword says to call the base class function. Without the virtual keyword, the compiler will call the derived class function.

Next, a derived class has no idea how to intialize a base class so constructors are not inherited so you cannot override a base class constructor in a derived class. And that prevents a virtual constructor.

Lastly, factory classes are classes that can create various kinds of objects. Factory class methods sometimes are called virtual contructors.
Apr 15 '07 #2
The simple answer is that when a class is constructed, if it inherits from another class, then the base class must be constructed first and only then the inherited class.
If the constructor was virtual, polymorphism would prevent from the base class to be constructed first.

Thats the same reason why the destructor is virtual, because the inherited class is the first to be destructed and only then the base class.
Apr 16 '07 #3
stroken
24
...
Thats the same reason why the destructor is virtual, because the inherited class is the first to be destructed and only then the base class.
Correct me if I'm wrong, but the destructor isn't virtual by default,.. you have to specify virtual before the destructor.
Apr 16 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
You are correct. The destructor IS NOT virtual by default.

However, a virtual destructor does not mean the destructor is a virtual function in the virtual function table. Destructors are not inherited. All it means is that when the object is destroyed by using a BASE class pointer or reference that the DERIVED class destructor is called and then the base class destructor is called. Without this signal to the compiler, deleting an object by using a base class pointer will call only the base class destructor.

Example:
class Base
{
public:
~Base();
};
class Derived : public Base
{
public:
~Derived()
};

and you:

Base* obj = new Derived;
delete obj; //only Base::~Base() is called

Change things this way:

class Base
{
public:
virtual ~Base();
};
class Derived : public Base
{
public:
~Derived()
};

and you:

Base* obj = new Derived;
delete obj; //only Derived::~Derived() called
// and then Base::~Base() is called.
Apr 16 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message...
11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
2
by: Yasutaka Ito | last post by:
Hi folks! I have a BaseForm class that inherits System.Windows.Forms.Form. It has a property, whose value I need supplied by the class that inherits it. The BaseForm usees the value supplied...
6
by: Thomas Matthews | last post by:
Hi, Is placing the keyword "virtual" in front of a constructor allowed as in the sample below? class TTable { virtual TTable(); }; My compiler, Borland Builder 5.2, has system libraries
3
by: ccs | last post by:
In Meyers' book he gave an example of "virtual copy constructor", which is quite different to an "ordinary" copy constructor by: 1. it returns a pointer to an object instead of a reference. 2. it...
3
by: ernesto | last post by:
Hi everybody: I have the following implementations: class A { public: virtual int GetValue() = 0; };
8
by: Dev | last post by:
Hello, Why an Abstract Base Class cannot be instantiated ? Does anybody know of the object construction internals ? What is the missing information that prevents the construction ? TIA....
4
by: Jeff Mallett | last post by:
VC++.NET gave me Compiler Error C2621, which states, "A union member cannot have a copy constructor." Yikes, that can't be true! As I understand it, *all* class objects have copy constructors,...
12
by: siddhu | last post by:
Hello, would you suggest to me, why gcc 3.3.3 can not compile this: template<class T> class Base { Base(){} friend T; };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.