473,785 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual constructor

Hi,
I have doubts reg virtual constructor
what is virtual constructor?
Is c++ supports virtual constructor?
Can anybody explain me about virtual constructor?
Regards,
Mani

Mar 14 '06
16 3759
"Alf P. Steinbach" <al***@start.no > wrote in message
news:47******** ****@individual .net...
* Jim Langston:
"Alf P. Steinbach" <al***@start.no > wrote in message
news:47******** ****@individual .net...
* pl**********@gm ail.com:
I have doubts reg virtual constructor
Note: "doubt" means you are unsure whether something is true or not, or
whether a person is truthful or reliable, or not.

Your usage above is meaningless and probably due to a bad dictionary.


Actually, it's probably because he's Norweigian and English is not his
primary language.
what is virtual constructor?
See the FAQ. Use the FAQ's Google search (it's on the main page of the
FAQ) to find the phrase "virtual constructor" in the FAQ. Apart from
that meaning, which has only a peripheral connection to the words
"virtual" and "constructo r" (it's more like a random phrase that for
some historical reason has acquired a given meaning), the FAQ also
discusses another more natural meaning; search the FAQ for the phrase
"Dynamic Binding During Initialization" to find that discussion.

Is c++ supports virtual constructor?
Yes.


No. From the FAQ 20.8 in part:

An idiom that allows you to do something that C++ doesn't directly
support.
Can anybody explain me about virtual constructor?
Yes, see the FAQ.


Yes, see the FAQ 20.8 You can't have a virtual constructor, but you can
have something that mimics what a virtual constructor would do.

Again, FAQ 20.8 in a bigger part:
You can get the effect of a virtual constructor by a virtual clone()
member function (for copy constructing), or a virtual create() member
function (for the default constructor).

Here is the link to the FAQ 20.8:
http://www.parashift.com/c++-faq-lit....html#faq-20.8


I'm not sure what point (if any) you were trying to make? Confuse the
newbie, and remove his or her learning experience in searching the FAQ?
Look how smart I am, I managed to find it in the FAQ?

Anyway, your unqualified "no" above is incorrect. C++ does indeed support
all that's needed to implement what's known as a virtual constructor,
including virtual member functions and function result covariance. Note
that this does not contradict the FAQ's statement.


I read the OP's question as: I do not know if C++ would allow a virtual
constructor, but if it did, what would be it's purpose? What would making a
constructor virtual accomplish?

Which is why the two part answer. C++ directly does not support a virtual
constructor. Which is an answer of No. But, you could do what a virtual
constructor would supposedly do by using a virtual clone() or create()
member function which is what people think a virtual constructor would do if
C++ in fact supported it.

As far as learning experience in searching in the FAQ, I'm sure that 95% of
the questions asked in this newsgroup could be answered by STFW, if not 100%
of them.
Mar 14 '06 #11
hi alf ,

of course you can simulate 'virtual ' constructor, but there is no
'direct' support(from compiler) like it is for virtual destructor.

Mar 14 '06 #12
* Jim Langston:
"Alf P. Steinbach" <al***@start.no > wrote in message
news:47******** ****@individual .net...
* Jim Langston:
"Alf P. Steinbach" <al***@start.no > wrote in message
news:47******** ****@individual .net...
* pl**********@gm ail.com:
> I have doubts reg virtual constructor
Note: "doubt" means you are unsure whether something is true or not, or
whether a person is truthful or reliable, or not.

Your usage above is meaningless and probably due to a bad dictionary.
Actually, it's probably because he's Norweigian and English is not his
primary language.

> what is virtual constructor?
See the FAQ. Use the FAQ's Google search (it's on the main page of the
FAQ) to find the phrase "virtual constructor" in the FAQ. Apart from
that meaning, which has only a peripheral connection to the words
"virtual" and "constructo r" (it's more like a random phrase that for
some historical reason has acquired a given meaning), the FAQ also
discusses another more natural meaning; search the FAQ for the phrase
"Dynamic Binding During Initialization" to find that discussion.

> Is c++ supports virtual constructor?
Yes.
No. From the FAQ 20.8 in part:

An idiom that allows you to do something that C++ doesn't directly
support.

> Can anybody explain me about virtual constructor?
Yes, see the FAQ.
Yes, see the FAQ 20.8 You can't have a virtual constructor, but you can
have something that mimics what a virtual constructor would do.

Again, FAQ 20.8 in a bigger part:
You can get the effect of a virtual constructor by a virtual clone()
member function (for copy constructing), or a virtual create() member
function (for the default constructor).

Here is the link to the FAQ 20.8:
http://www.parashift.com/c++-faq-lit....html#faq-20.8 I'm not sure what point (if any) you were trying to make? Confuse the
newbie, and remove his or her learning experience in searching the FAQ?
Look how smart I am, I managed to find it in the FAQ?

Anyway, your unqualified "no" above is incorrect. C++ does indeed support
all that's needed to implement what's known as a virtual constructor,
including virtual member functions and function result covariance. Note
that this does not contradict the FAQ's statement.


I read the OP's question as: I do not know if C++ would allow a virtual
constructor, but if it did, what would be it's purpose? What would making a
constructor virtual accomplish?


Don't inform me. Instead, bring your interpretation with your response,
if it has any bearing on the response. Anyway, I think it's a Good Idea
to assume a question is meaningful, like the established meaning for the
actual words used by the OP, rather than something meaningless.

Which is why the two part answer. C++ directly does not support a virtual
constructor. Which is an answer of No. But, you could do what a virtual
constructor would supposedly do by using a virtual clone() or create()
member function which is what people think a virtual constructor would do if
C++ in fact supported it.
Also here. There are several possible meanings for "virtual
constructor" apart from the established meaning, most notably the one of
doing virtual construction (where part of the construction is specified
by a derived class; directly supported in Java and C# by breaking those
languages' type safety, and possible to simulate in C++, see the FAQ).
But yours isn't one: a constructor can't do the job of a clone function.

As far as learning experience in searching in the FAQ, I'm sure that 95% of
the questions asked in this newsgroup could be answered by STFW, if not 100%
of them.


Does that mean "Searching The Fucking Web"? Language, Jim. Also,
that's highly misleading: nobody tried to send anyone on a wild-goose
chase, as you imply, but instead the OP was directed on a highly focused
search guaranteed to yield a useful result.

But you then stepped in with a totally unncessary "clear up things"
posting which in addition to depriving the OP the chance to learn to
something, was factually incorrect, and as you now clarify, relied on a
meaningless interpretation of the question rather than assuming some
meaning in there.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 14 '06 #13
* al pacino:
hi alf ,

of course you can simulate 'virtual ' constructor, but there is no
'direct' support(from compiler) like it is for virtual destructor.


It /is/ possible to quote using Google groups, but I think you have to
press some button or some such (please check that out).

Anyway, there's no need to "simulate" a "virtual" constructor.

The phrase "virtual constructor" has an established meaning, and can be
expressed directly in C++ without any simulation at all; see the FAQ.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 14 '06 #14
Alf P. Steinbach wrote:
I read the OP's question as: I do not know if C++ would allow a virtual
constructor, but if it did, what would be it's purpose? What would
making a constructor virtual accomplish?
Don't inform me. Instead, bring your interpretation with your response,
if it has any bearing on the response. Anyway, I think it's a Good Idea
to assume a question is meaningful, like the established meaning for the
actual words used by the OP, rather than something meaningless.

Which is why the two part answer. C++ directly does not support a
virtual constructor. Which is an answer of No. But, you could do what a
virtual constructor would supposedly do by using a virtual clone() or
create() member function which is what people think a virtual constructor
would do if C++ in fact supported it.


Also here. There are several possible meanings for "virtual constructor"
apart from the established meaning,


Those other meanings are based on incorrect interpretations of the word
"virtual". The GoF book calls it "factory method", which is a much better
name for it.
most notably the one of doing virtual construction (where part of the
construction is specified by a derived class; directly supported in Java
and C# by breaking those languages' type safety, and possible to simulate
in C++, see the FAQ).
But yours isn't one: a constructor can't do the job of a clone function.
A constructor can't do the job of a "virtual constructor" either. What the
FAQ mentions is not a constructor or something that does the job of a
constructor. The clone() function would even be closer to something like a
virtual constructor than a factory, because it can at least take the
dynamic type of an existing object, as virtual functions do. A factory
needs some other means to decide with class to instantiate.
As far as learning experience in searching in the FAQ, I'm sure that 95%
of the questions asked in this newsgroup could be answered by STFW, if
not 100% of them.


Does that mean "Searching The Fucking Web"? Language, Jim.


You were the one who used the word.
Also, that's highly misleading: nobody tried to send anyone on a
wild-goose chase, as you imply, but instead the OP was directed on a
highly focused search guaranteed to yield a useful result.

But you then stepped in with a totally unncessary "clear up things"
posting
Clearing things up is not unnecessary, especially not in a technical forum.
which in addition to depriving the OP the chance to learn to something,
was factually incorrect,
It was correct.
and as you now clarify, relied on a meaningless interpretation of the
question rather than assuming some meaning in there.


The C++ standard's definition of the word "virtual" is not at all
meaningless in a C++ newsgroup.

Mar 14 '06 #15
* Rolf Magnus:
Alf P. Steinbach wrote:
I read the OP's question as: I do not know if C++ would allow a virtual
constructor, but if it did, what would be it's purpose? What would
making a constructor virtual accomplish? Don't inform me. Instead, bring your interpretation with your response,
if it has any bearing on the response. Anyway, I think it's a Good Idea
to assume a question is meaningful, like the established meaning for the
actual words used by the OP, rather than something meaningless.

Which is why the two part answer. C++ directly does not support a
virtual constructor. Which is an answer of No. But, you could do what a
virtual constructor would supposedly do by using a virtual clone() or
create() member function which is what people think a virtual constructor
would do if C++ in fact supported it.

Also here. There are several possible meanings for "virtual constructor"
apart from the established meaning,


Those other meanings are based on incorrect interpretations of the word
"virtual".


Nope, sorry, that's incorrect.

The GoF book calls it "factory method", which is a much better name for it.
The GoF book calls /what/ the "factory method"?

Anyway, qualified names are good, so now we can speak of "GOF factory
method", as soon as we've checked that it's at all relevant.

most notably the one of doing virtual construction (where part of the
construction is specified by a derived class; directly supported in Java
and C# by breaking those languages' type safety, and possible to simulate
in C++, see the FAQ).
But yours isn't one: a constructor can't do the job of a clone function.


A constructor can't do the job of a "virtual constructor" either.


A "virtual constructor" /is/ a clone function, in the established
meaning; see the FAQ.

What the
FAQ mentions is not a constructor or something that does the job of a
constructor. The clone() function would even be closer to something like a
virtual constructor than a factory, because it can at least take the
dynamic type of an existing object, as virtual functions do. A factory
needs some other means to decide with class to instantiate.


The FAQ mentions several things.

What on Earth are you, uh, /talking/ about?

As far as learning experience in searching in the FAQ, I'm sure that 95%
of the questions asked in this newsgroup could be answered by STFW, if
not 100% of them.

Does that mean "Searching The Fucking Web"? Language, Jim.


You were the one who used the word.


That's a lie.

Also, that's highly misleading: nobody tried to send anyone on a
wild-goose chase, as you imply, but instead the OP was directed on a
highly focused search guaranteed to yield a useful result.

But you then stepped in with a totally unncessary "clear up things"
posting


Clearing things up is not unnecessary, especially not in a technical forum.


Confusing things is.
which in addition to depriving the OP the chance to learn to something,
was factually incorrect,


It was correct.


Nope, sorry, you're incorrect.

and as you now clarify, relied on a meaningless interpretation of the
question rather than assuming some meaning in there.


The C++ standard's definition of the word "virtual" is not at all
meaningless in a C++ newsgroup.


What are you babbling about?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 14 '06 #16
Alf P. Steinbach wrote:
* Rolf Magnus:
Alf P. Steinbach wrote:
I read the OP's question as: I do not know if C++ would allow a virtual
constructor, but if it did, what would be it's purpose? What would
making a constructor virtual accomplish?
Don't inform me. Instead, bring your interpretation with your response,
if it has any bearing on the response. Anyway, I think it's a Good Idea
to assume a question is meaningful, like the established meaning for the
actual words used by the OP, rather than something meaningless.
Which is why the two part answer. C++ directly does not support a
virtual constructor. Which is an answer of No. But, you could do what
a virtual constructor would supposedly do by using a virtual clone() or
create() member function which is what people think a virtual
constructor would do if C++ in fact supported it.
Also here. There are several possible meanings for "virtual
constructor" apart from the established meaning,
Those other meanings are based on incorrect interpretations of the word
"virtual".


Nope, sorry, that's incorrect.

The GoF book calls it "factory method", which is a much better name for
it.


The GoF book calls /what/ the "factory method"?


Under the description of "factory method", it says "also known as virtual
constructor". And while we're at it, from TC++PL:
<refering to a factory class>
"Such a class is sometimes called a factory, and its functions are (somewhat
misleadingly) sometimes called virtual constructors.

I thought that was what you meant by "virtual constructor", which seems to
be not the case.
Anyway, qualified names are good, so now we can speak of "GOF factory
method", as soon as we've checked that it's at all relevant.


Well, we could also call it the Steinbach virtual constructor then.
most notably the one of doing virtual construction (where part of the
construction is specified by a derived class; directly supported in Java
and C# by breaking those languages' type safety, and possible to
simulate in C++, see the FAQ).
But yours isn't one: a constructor can't do the job of a clone function.


A constructor can't do the job of a "virtual constructor" either.


A "virtual constructor" /is/ a clone function, in the established
meaning; see the FAQ.


What makes you think that the FAQ represents "the established meanging"?
Anyway, I have to admit that I didn't look at the FAQ's explanations of
"virtual constructor", because I thought it must be the same as what the
GoF and Stroustrup say. Considering that it actually isn't, the meaning
described in the FAQ doesn't seem so well-established after all, does it?
What the
FAQ mentions is not a constructor or something that does the job of a
constructor. The clone() function would even be closer to something like
a virtual constructor than a factory, because it can at least take the
dynamic type of an existing object, as virtual functions do. A factory
needs some other means to decide with class to instantiate.


The FAQ mentions several things.

What on Earth are you, uh, /talking/ about?


Sorry, I was talking about another "establishe d meaning" of the term.
As far as learning experience in searching in the FAQ, I'm sure that
95% of the questions asked in this newsgroup could be answered by STFW,
if not 100% of them.
Does that mean "Searching The Fucking Web"? Language, Jim.


You were the one who used the word.


That's a lie.


At least you show some humor :)
Also, that's highly misleading: nobody tried to send anyone on a
wild-goose chase, as you imply, but instead the OP was directed on a
highly focused search guaranteed to yield a useful result.

But you then stepped in with a totally unncessary "clear up things"
posting


Clearing things up is not unnecessary, especially not in a technical
forum.


Confusing things is.


Then stop doing that.
which in addition to depriving the OP the chance to learn to something,
was factually incorrect,


It was correct.


Nope, sorry, you're incorrect.


From the C++ standard: "A constructor shall not be virtual". Whatever some
other definition (be it established or not) says is irelevant.
and as you now clarify, relied on a meaningless interpretation of the
question rather than assuming some meaning in there.


The C++ standard's definition of the word "virtual" is not at all
meaningless in a C++ newsgroup.


What are you babbling about?


Jim was interpreting the term according to the C++ standard ("you can't have
a virtual constructor"), and you called that interpretation meaningless.

Mar 14 '06 #17

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

Similar topics

0
4257
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message (with no responces associated) :
11
10526
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor" is called in Case 2 since only ~base() becomes "virtual" and ~Derived() is still non-virtual? 3. Does Case 3 show that we don't need any virtual destructor to make ~Derived() called? 4. Is "virtual destructor" needed only for Case 2?
6
2635
by: Thomas Matthews | last post by:
Hi, Is placing the keyword "virtual" in front of a constructor allowed as in the sample below? class TTable { virtual TTable(); }; My compiler, Borland Builder 5.2, has system libraries
3
5180
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 have empty argument list. 3. it has "virtual" keyword in front of it. My questions are: 1. How could "virtual" be used in front of a constructor even though it's a "copy constructor"? 2. Can an "ordinary" copy constructor be "virtual?
22
2963
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give me some hints on what might be wrong? // BEGIN OF SAMPLE CODE class Strategy
4
2345
by: Stefan Nikolaus | last post by:
Hello, I've run into problems with defining a template, which inherits from a base template and the usage of virtual methods in those. I want to initialize a member variable depending on which template is created and I tried to define a virtual method, that's called from the base template ctor. Here's my example code: #include <iostream>
3
2358
by: Klaas Vantournhout | last post by:
Hi, Recently I obtained a problem with virtual inheritance when implementing it in multiple files. To present the problem I have included at the bottom of this post the code of the 4 files. I show results with the gnu compiler but i have the same results with the intel compiler. file1.cpp compiles without a problem, the problem occurs when compiling file2.cpp. It complains about not finding the constructor of the class base.
1
3476
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } // A virtual destructor
17
3549
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9956
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.