virtual copy constructor

C

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!
 
J

John Harrison

ccs said:
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
 
L

Luther Baker

ccs said:
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
 
R

Rolf Magnus

ccs said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top