471,075 Members | 762 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

virtual copy constructor

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
3 5013

"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
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
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.

Similar topics

26 posts views Thread by pmizzi | last post: by
15 posts views Thread by Heiner | last post: by
16 posts views Thread by plmanikandan | last post: by
3 posts views Thread by Marcin Kalicinski | last post: by
4 posts views Thread by Corno | last post: by
1 post views Thread by Bart Simpson | last post: by
4 posts views Thread by Rahul | last post: by

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.