GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Tech FAQs  >  OOPS
Go To First  |  Previous Question  |  Next Question 
 OOPS  |  Question 238 of 255    Print  
Why we cant use constructor with virtual functions

  
Total Answers and Comments: 4 Last Update: November 29, 2006     Asked by: gaurav 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
November 08, 2006 04:12:49   #1  
bijal Member Since: January 2006   Contribution: 6    

RE: Why we cant use constructor with virtual functions...

The Virtual term tends to be overridable.Whenever in base class function written with virtual can only be overriden. Also the signature including function name and parameters must be same for virtual functions.

While this is not possible with constructors because constructors have same name as class,and no two constructors of base-derived classes can have same name.

And the other reason is, Constructors are special method that gets called automatically unlike other methods.

Even if i'll instantiate an object of derieved class,base class constructor get invoke first then only derieved one.

So while instatiating object of base or derieved will fire base constructor. That's why we can't use constructor as a virtual function.


 
Is this answer useful? Yes | No
November 17, 2006 04:18:37   #2  
vmmusthafa Member Since: November 2006   Contribution: 1    

RE: Why we cant use constructor with virtual functions...
constructor is with respect to base class
 
Is this answer useful? Yes | No
November 28, 2006 07:24:26   #3  
Dharma        

RE: Why we cant use constructor with virtual functions...
Hi bjal,Virtual constructors are still logically valid though we cannot have them.Virtual functions does not need to have same prototype always. Virtual destructors do not have same name. Also, they too will be called implicitly like constructors.I feel that, virtual constructor concept itself is not needed anywhere. That's why we can not have them.
 
Is this answer useful? Yes | No
November 29, 2006 01:20:00   #4  
Jayanta Raychowdhury        

RE: Why we cant use constructor with virtual functions...

The answer is when instantiating an object compiler needs a correct object type. If the constructor is declared virtual then the the object type can not be resolved at compile time, and that's why it is not possible to declare a constructor as virtual. But we can call a constructor within a virtual function in the base class to make it look virtual.


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape