By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,560 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

virtual copy constructor

P: n/a
ccs
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?

Thanks in advance!
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"ccs" <cc*@stopspamming.com> wrote in message
news:YI******************@bgtnsc04-news.ops.worldnet.att.net...
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.
4. And it isn't a constructor

My questions are:
1. How could "virtual" be used in front of a constructor even though it's a "copy constructor"?
It isn't a constructor, its just used in a similar manner.
2. Can an "ordinary" copy constructor be "virtual?


No constructors cannot be virtual under any circumstances.

Read your book again, I think you missed the point. The 'constructor' in the
example is not a real constructor, its a normal member function.

john
Jul 22 '05 #2

P: n/a
ccs wrote:
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.
4. He calls it clone().

He's using the term "constructor" loosely here. He's constructing and/or
copying objects ... things one normally associates with constructors.
My questions are:
1. How could "virtual" be used in front of a constructor even though it's a
"copy constructor"?
See above. He's written a normal, virtual method that happens to return
a pointer to an object of the class it is declared within. Its
*logically* a constructor, but syntactically, its just a virtual method.
2. Can an "ordinary" copy constructor be "virtual?


12.1p4:

A constructor shall not be *virtual* or *static*...

-Luther
Jul 22 '05 #3

P: n/a
ccs wrote:
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"?
The name "virtual copy constructor" doesn't mean it's a constructor
(which is why I don't like that name much).
2. Can an "ordinary" copy constructor be "virtual?


No. Constructors can't be virtual. A virtual function behaves
polymorphically, i.e. if you call it through a pointer to a base class
that points to an instance of a class derived from it, the
implementation of that derived class is called. This wouldn't make any
sense for constructors, since they are used to create the object. There
is no object before the constructor is called, and it basically gets
its type because the constructor of that type is called. So how would
the compiler decide which constructor to call in the case of a virtual
one?

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.