473,382 Members | 1,376 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,382 software developers and data experts.

what is the use of virtual constructors

1
hi all
I am new to the C++ please help me what is the use of virtual constructors in c++, and when we can use these virtual constructors.
Oct 27 '06 #1
2 1519
Banfa
9,065 Expert Mod 8TB
I have never seen, nor do I believe that it is correct syntax to have, a virtual constructor.
Oct 27 '06 #2
vermarajeev
180 100+
hi all
I am new to the C++ please help me what is the use of virtual constructors in c++, and when we can use these virtual constructors.
VIrtual constructor is a concept which doesnot exist in C++ but that provides you to implement one.

First thing first
Why do we require a virtual construct?
When you just consider virtual functions, it is concept which is completely based on type of object (not the type of pointer/reference to that object). ie the functions are called at runtime not compile time.

Now conside this example
Base* b = new Derived();

Hope I'm clear and not mistaken. If I'm wrong please comment.

Unlike ordinary member function, a constructor allows you to construct the type of the object at compile time. In above case, the complete object is created at compile time , this does not delay till runtime. So, the concept of virtual constructor is vague in this concept.

But you can provide the concept using virtual functions
Conside this example

Expand|Select|Wrap|Line Numbers
  1. class Browser
  2. {
  3.     public: 
  4.      Browser(){}
  5.      Browser(const Browser&){}  
  6.         virtual Browser* construct()
  7.         {
  8.             return new Browser();
  9.         }
  10.  
  11.         virtual Browser* clone()
  12.         {
  13.             return new Browser(*this);
  14.         }
  15.       virtual ~Browser(){}
  16. };
  17.  
  18. class HTML : public Browser
  19. {
  20.   public:
  21.      HTML(){}
  22.      HTML(const HTML&){}  
  23.         HTML* construct()
  24.         {
  25.             return new HTML();
  26.         }
  27.  
  28.         HTML* clone()
  29.         {
  30.             return new Browser(*this);
  31.         }
  32.       virtual ~HTML(){}
  33. };
  34.  
  35. void userCode( Browser& b)
  36. {
  37.     Browser* bw = b.construct();
  38.     //use bw
  39.     delete bw;
This allows you to instatiate a new object of the right type without having to know the type of source object.
Oct 27 '06 #3

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

Similar topics

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...
4
by: rahul8143 | last post by:
hello, i want to know that is virtual constructors possible? if not why? also does virtual destructors functions are often used in C++ programming. regards, rahul.
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
4
by: Rahul | last post by:
Hi Everyone, I understand that the constructors can't be virtual and parashift has the following example, to have an workaround for the constructors to be virtual, class Shape { public:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.