473,396 Members | 2,014 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,396 software developers and data experts.

Construtors

Hi all
I want to know reasons behind why we cannot delcare construtors virtual

Thanks

Apr 5 '06 #1
3 1662
amit tikoo wrote:
Hi all
I want to know reasons behind why we cannot delcare construtors virtual

Thanks


The only reason is because the standard doesn't allow it. One reason
why the standard may not allow it is because it is reasonably easy to
achieve the same thing using the following idiom, and therefore there is
little value in changing the language specification:
http://www.parashift.com/c++-faq-lit....html#faq-20.8

Alan
Apr 5 '06 #2
On 5 Apr 2006 01:42:00 -0700, "amit tikoo" <am*******@gmail.com>
wrote:
Hi all
I want to know reasons behind why we cannot delcare construtors virtual

Well, let's see:

Virtual functions take their value right before constructor is
invoked. So, it seems there would be no problem, as the constructor
would be available when it should be invoked.

But it is not so easy. let's see the following inheritance
supposition, with virtual constructors available:

struct A {
virtual A(int i);
};

struct B:public A {
virtual B(int i);
};

B sample(5);
What happens when trying to construct "sample"?

1.- Memory is allocated, enough for struct B.
2.- Contructor invoked? But which one? B constructor will not be
available until A is fully constructed. So, A will begin to be
constructed, but how? As B constructor is not available, there is
either no way to know how to construct A, or the virtual constructor
available is A constructor and this will be the one called, never
calling B constructor.

So, point 2 reflects the paradox of virtual constructors and why C++
language cannot support them as a secondary effect of the standar
construction order.

That is the reason to use Factory methods for polimorphic creation of
objects.

Best regards,

Zara
Apr 5 '06 #3
amit tikoo wrote:
Hi all
I want to know reasons behind why we cannot delcare construtors virtual


This is asked every week:

http://groups.google.co.uk/groups/se...r%22&scoring=d

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Apr 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: tsteinke | last post by:
Doe anyone know the way to explicitly call the varible initializers on an object? for instance Class A { private int a=123; }
4
by: wenjie wang | last post by:
Greetings, I'd like to know is there any situation where your program could crash without reaching the breakpoint which you set at the beginning of main().
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
0
by: Mas L via DotNetMonster.com | last post by:
hi, I am a C#(2005) developer trying to score with VS C++(2005). 1. This is what I do, but I get an error and what to have an Try catch (RemotingException e)... ...
7
by: John Grandy | last post by:
My ASP.NET Web Service project has a Web Method that returns an array filled with instances of a custom class. The custom class is defined in a Class Library that is included in the web-service...
7
by: Holger Boskugel | last post by:
Hello, I'm looking for a solution to hide the default constructor of a class. In VB6 there was a class type of "Public Not Creatable". I have tried to write the default ctor as friend or private...
15
by: pattanaikhr | last post by:
How can we get methods name and costructors name from java class before compilation?Please any body have any idea plaese share with me
4
by: john | last post by:
Hey guys, Thank you for your input from the last topic. I tried referencing my c+ + book and googled the topic but i'am still confused a bit. I have another problem with that same code. I was...
2
by: santhoshcz | last post by:
Is it so the only reason we cannot call it explicitly or it has some other reason?? Thanks, Santhosh.
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.