Click here to Skip to main content
15,886,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Is it possible to create 'Virtual Constructor' programetically in C++.
I don't think so.
If you have any concrete idea on this then please share it.Thanks
Posted

There is a concept of virtual constructor in some languages where is it extremely useful. One example is Delphi Pascal, see http://en.wikipedia.org/wiki/Embarcadero_Delphi[^].

In C++ virtual constructors are not allowed and such techniques are not applicable. The closest possible concept is factory, especially abstract factory. Please see http://en.wikipedia.org/wiki/Factory_method_pattern[^], http://en.wikipedia.org/wiki/Abstract_factory_pattern[^].

Specifically to C++, please see ++:
http://sourcemaking.com/design_patterns/factory_method/cpp/1[^],
http://sourcemaking.com/design_patterns/abstract_factory/cpp/1[^],
http://sourcemaking.com/design_patterns/abstract_factory[^],
http://login2win.blogspot.com/2008/05/c-factory-pattern.html[^].

That was about classical factory approaches.
This CodeProject article shows a different generic approach:
Generic Class Factory[^].

—SA
 
Share this answer
 
v5
Comments
Espen Harlinn 29-May-11 16:42pm    
Well answered, my 5
Sergey Alexandrovich Kryukov 29-May-11 16:57pm    
Thank you Espen.
I see we answered one more C++ question, next one.
--SA
ShilpiP 7-Jun-11 3:36am    
You deserve +5 :)
Sergey Alexandrovich Kryukov 7-Jun-11 3:37am    
Thank you.
--SA
There is no virtual constructor in C++, But why you need it.

If you look what the virtual distructor do, then you find that Actually there is no need (meaning) of virtual constructor.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 7-Jun-11 0:32am    
Lie. However, I understand it's not intentional but based on lack of understanding OOP in wider scope then C++, created much later then fully-fledged variants of OOP.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900