473,327 Members | 2,071 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,327 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 5140

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
15
by: Heiner | last post by:
#include <stdio.h> class A { public: virtual A & operator= (const A &); virtual void test(const A &); }; class B : public A
16
by: plmanikandan | last post by:
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
3
by: Marcin Kalicinski | last post by:
Hi, Having the following program: #include <iostream> struct A { A() { std::cout << "A::A()\n"; } A(const A &) { std::cout << "A::A(const A &)\n"; }
4
by: Corno | last post by:
Hi all, I thought that the 2 following functions would have the same effect; void first() { ClassA a("bla"); anotherFunction(a); }
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
1
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() { } ...
1
by: Visame | last post by:
#include <iostream> using namespace std; class A { public: A() {cout << "constructor A" << endl;} A(A& a) {cout<<"copy A"<<endl;} virtual void Test(){cout <<"A test"...
4
by: Rahul | last post by:
Hi Everyone, I understand that the constructors can't be virtual and parashift has the following example, to have an workaround for the constructors to be virtual, class Shape { public:...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.