Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on Virtual Constructor within the C and C++ forums, part of the Software Development category; In C++ there is a concept of Virtual Distributor but there is no virtual constructor or constructor are not virtual why??????...
|
|||||||
|
|||
|
Virtual Constructor
In C++ there is a concept of Virtual Distributor but there is no virtual constructor or constructor are not virtual why??????
Last edited by AnisHasan; 07-19-2009 at 03:04 PM. |
| The Following User Says Thank You to AnisHasan For This Useful Post: | ||
| Sponsored Links |
|
|||
|
Re: Virtual Constructor
Hi,
In C++, Virtual is Used foe polymorphism, which is calling a Derived class function using the Base class pointer. Since all the functions in the Public, protected are inherited we may have virtual functions. BUT Constructor of a class is never and ever extended by a derived, hence we will never heed to have a Virtual constructor..... ie . U will never need to call a derived class constructor using the Base class Pointer... Thanks & Regards, Riju. |
|
|||
|
Re: Virtual Constructor
While it is true that using the keyword 'virtual' when declaring a constructor results in a syntax error at compile time, the concept of a virtual constructor exits in C++ as a design pattern, sometimes called the Factory Method. See Duffy "Financial Instrument Pricing Using C++."
|
|
|||
|
Re: Virtual Constructor
Hi,
A destructor can be defined as virtual or even pure virtual. You would use a virtual destructor if you ever expect a derived class to be destroyed through a pointer to the base class. This will ensure that the destructor of the most derived classes will get called: Eg: A* b1 = new B; delete b1; If A does not have a virtual destructor, deleting b1 through a pointer of type A will merely invoke A's destructor. To enforce the calling of B's destructor in this case we must have specified A's destructor as virtual: virtual ~A(); Thanks, Riju. |
|
|||
|
Re: Virtual Constructor
The concept of polymorphism is by itself done in the constructor of each class.
The constructor owns the responsibility of initialising the Vtable and vptr which actually implements the Polymorphism technology. So constructor cannot be virtual. And it is also not required. What is the concept of Polymorphism??? to call the respective functions of the object at run time rather than the static type of the object. Imagine if I need to create an inheritted class object and the call does not go the base class constructor?? will my object be complete... then y is the necessity for such.... Virtual destructor is a must eg Base *b; b = new Derived(); delete b; // if my destructor is not virtual then it will invoke Base destructor this is a big mess. Suppose I make them virtual then my call goes to derived. |
|
|||
|
Re: Virtual Constructor
Neither constructors nor destructors are inherited.
Though virtual destructors are used for polymorphic approach in case of inheritance, but the main reason of using virtual destrustor is used for effective memory management. It makes sure that all the relevant memory released associated with the class & its subclasses at the time of stack unwinding when destructor is called. |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| constructor | praveenkola | C and C++ | 2 | 03-16-2009 09:55 AM |
| constructor | emb.mallik | C and C++ | 1 | 03-02-2009 04:50 PM |
| C++ Pure Virtual Function and Virtual Base Class | Lokesh M | C and C++ | 0 | 10-04-2007 07:29 AM |
| why i am getting an error while calling a constructor from another constructor | raj_java_j2ee | Java | 4 | 07-22-2007 03:39 AM |
| A derivation inherits both a virtual and non-virtual instance | Geek_Guest | OOPS | 1 | 07-18-2007 05:51 AM |