Geeks Talk

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.

Virtual Constructor

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??????...

Go Back   Geeks Talk > Software Development > C and C++
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 07-19-2009
Junior Member
 
Join Date: Jul 2009
Location: Noida
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
AnisHasan is on a distinguished road
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.
Reply With Quote
The Following User Says Thank You to AnisHasan For This Useful Post:
Sponsored Links
  #2 (permalink)  
Old 07-20-2009
Expert Member
 
Join Date: May 2009
Location: Bangalore
Posts: 989
Thanks: 155
Thanked 420 Times in 201 Posts
rijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nice
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.
Reply With Quote
  #3 (permalink)  
Old 07-29-2009
Junior Member
 
Join Date: Jul 2009
Location: New York
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
bfeingold is on a distinguished road
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++."
Reply With Quote
  #4 (permalink)  
Old 08-18-2009
Junior Member
 
Join Date: Aug 2009
Location: chennai
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
333immanuell is on a distinguished road
Re: Virtual Constructor

what is virtual distructor?
Reply With Quote
  #5 (permalink)  
Old 08-18-2009
Expert Member
 
Join Date: May 2009
Location: Bangalore
Posts: 989
Thanks: 155
Thanked 420 Times in 201 Posts
rijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nice
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.
Reply With Quote
  #6 (permalink)  
Old 4 Weeks Ago
Junior Member
 
Join Date: Jun 2009
Location: India
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
BeulahJerald is on a distinguished road
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.
Reply With Quote
  #7 (permalink)  
Old 4 Weeks Ago
Junior Member
 
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
jitu2233 is on a distinguished road
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.
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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


All times are GMT -4. The time now is 09:44 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved