473,396 Members | 1,866 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,396 software developers and data experts.

Question about using copy constructor of parent class?

In the programme below is it possible to call copy constructor of class A,
inside copy constructor of class B.
#include <iostream>
using namespace std;
class A{
int a;
public:
A(int temp=0):a(temp){}
A(A& right):a(right.a){}
};
class B:public A{
int b;
public:
B(int temp=0):b(temp){}
B(B& right){ A(right.A);
}
};
int main(){
}

Jul 20 '06 #1
5 2297
flamexx7 wrote:
In the programme below is it possible to call copy constructor of class A,
inside copy constructor of class B.
Yes, but not for the current object. You can do that in the initializer
list.
#include <iostream>
using namespace std;
class A{
int a;
public:
A(int temp=0):a(temp){}
A(A& right):a(right.a){}
Should be:

A(const A& right):a(right.a){}

Or even better, just leave it out completely. The compiler will then
generate a copy constructor for you that does exactly the same.
};
class B:public A{
int b;
public:
B(int temp=0):b(temp){}
B(B& right){ A(right.A);
}
B(const B& right):A(right) {}

};
int main(){
}
Jul 20 '06 #2
In my book there is a question about "properly" creating a copy constructor
of child class during inheritance. Is it ok to use upcasting here ? I've
used upcasting and it seems to work fine.

#include <iostream>
using namespace std;
class A{
int a;
public:
A(int temp=0):a(temp){}
A(A& right):a(right.a){}
};
class B:public A{
int b;
public:
B(int temp=0):b(temp){}
B(B& right){A(*this);
b=right.b;
}
};
int main(){
B b;
B b1=b;
cin.get();
}


"Rolf Magnus" <ra******@t-online.dewrote in message
news:e9*************@news.t-online.com...
flamexx7 wrote:
>In the programme below is it possible to call copy constructor of class
A,
inside copy constructor of class B.

Yes, but not for the current object. You can do that in the initializer
list.
>#include <iostream>
using namespace std;
class A{
int a;
public:
A(int temp=0):a(temp){}
A(A& right):a(right.a){}

Should be:

A(const A& right):a(right.a){}

Or even better, just leave it out completely. The compiler will then
generate a copy constructor for you that does exactly the same.
> };
class B:public A{
int b;
public:
B(int temp=0):b(temp){}
B(B& right){ A(right.A);
}

B(const B& right):A(right) {}

> };
int main(){
}

Jul 21 '06 #3

flamexx7 wrote:
In my book there is a question about "properly" creating a copy constructor
of child class during inheritance. Is it ok to use upcasting here ? I've
used upcasting and it seems to work fine.

#include <iostream>
using namespace std;
class A{
int a;
public:
A(int temp=0):a(temp){}
A(A& right):a(right.a){}
};
class B:public A{
int b;
public:
B(int temp=0):b(temp){}
B(B& right){A(*this);
b=right.b;
}
};
int main(){
B b;
B b1=b;
cin.get();
}
It compiles, but, although I don't know what you want it to do, it
doesn't look like it does what I think you want it to do.

In the copy constructor of B, you initialize A with *this, before B is
initialised. So, if you in main() do:

int main(){
B b(10);
B b1=b;

you'll get a B1 who's a is uninitialised (that's what it looks like to
me. I just compiled it with g++-3.4.4, and that shows that b1.a
initialised to whatever default value I put in A::A(int temp=xxx)
constructor.

#include <iostream>
using namespace std;
class A{
int a;
public:
A(int temp=123):a(temp){}
A(A& right):a(right.a){}
void show(){
cout<<"A: a="<<a<<endl;
}
};
class B:public A{
int b;
public:
B(int temp=124):b(temp){}
B(B& right){A(*this);
b=right.b;
}
void show(){
A::show();
cout<<"B: b="<<b<<endl;
}
};
int main(){
B b(10);
b.show();
B b1=b;
b1.show();
}

Output:

A: a=123
B: b=10
A: a=123
B: b=10
Oh, and BTW, why didn't you do as suggested, make the ref arguments
const?

Jul 21 '06 #4
flamexx7 wrote:
In my book there is a question about "properly" creating a copy
constructor of child class during inheritance. Is it ok to use upcasting
here ?
No need to cast.
I've used upcasting and it seems to work fine.

#include <iostream>
using namespace std;
class A{
int a;
public:
A(int temp=0):a(temp){}
A(A& right):a(right.a){}
};
class B:public A{
int b;
public:
B(int temp=0):b(temp){}
B(B& right){A(*this);
b=right.b;
}
This constructor probably won't do what you want. The

A(*this);

creates a new temprary object of type A and immediately afterwards, destroys
it. The A part of your B object gets default initialized. It has nothing at
all to do with the A in your constructor body. I'll repeat myself: You can
only initialize the base class parts of your object in the initializer
list. In the constructor body, the initialization is finished.
};
int main(){
B b;
B b1=b;
cin.get();
}
You tricked yourself, because the initialization you were trying to do just
happens to do the same as the default initialization - they both set the
int member to 0.

Jul 21 '06 #5

"flamexx7" <none@nonewrote in message
news:44***********************@news.sunsite.dk...
In my book there is a question about "properly" creating a copy
constructor of child class during inheritance. Is it ok to use upcasting
here ? I've used upcasting and it seems to work fine.

#include <iostream>
using namespace std;
class A{
int a;
public:
A(int temp=0):a(temp){}
A(const A& right):a(right.a){}
};
class B:public A{
int b;
public:
B(int temp=0):b(temp){}
B(const B& right){A(*this);
You're right rolf, I tricked myelf. Now I know instead of
B(const B& right){A(*this);
it should be

B(const B& right):A(right){

b=right.b;
}
};
int main(){
B b;
B b1=b;
cin.get();
}


"Rolf Magnus" <ra******@t-online.dewrote in message
news:e9*************@news.t-online.com...
>flamexx7 wrote:
>>In the programme below is it possible to call copy constructor of class
A,
inside copy constructor of class B.

Yes, but not for the current object. You can do that in the initializer
list.
>>#include <iostream>
using namespace std;
class A{
int a;
public:
A(int temp=0):a(temp){}
A(A& right):a(right.a){}

Should be:

A(const A& right):a(right.a){}

Or even better, just leave it out completely. The compiler will then
generate a copy constructor for you that does exactly the same.
>> };
class B:public A{
int b;
public:
B(int temp=0):b(temp){}
B(B& right){ A(right.A);
}

B(const B& right):A(right) {}

>> };
int main(){
}


Jul 22 '06 #6

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

Similar topics

15
by: Wolfram Humann | last post by:
Hi, please don't be too harsh if I made stupid errors creating this simple example from my more complex case. Suppose I have a class like this: class BOOK { const string title;
51
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class,...
2
by: Kamran | last post by:
Hi I have very little experience of C++, nevertheless I have been asked to write a gui using QT/QWT. I know that I should direct the question to the relevant mailing list and I have done that but...
3
by: Zootal | last post by:
I have a question about the syntax involved in inheritance. I have a parent class, and a child class. When I create an instance of the class, I pass to it 3 parameters. Two of them are used by the...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
61
by: Sanders Kaufman | last post by:
I'm wondering if I'm doing this right, as far as using another class object as a PHP class property. class my_baseclass { var $Database; var $ErrorMessage; var $TableName; var $RecordSet;...
5
by: Vijay | last post by:
Hi All, I am not able to figure out what exactly happening in below code. what is control flow. Can anyone clear my confusion? Code: class A { public: A(){cout<<"In Constructor\n";}
8
by: hill.liu | last post by:
Hi, I stuck into this problem that I can't figure it out. Here is the class definition: class ctest { public: ctest(void) { cout << "ctest default constor" << endl; }; ctest(ctest& c) {...
1
by: =?ISO-8859-1?Q?Andr=E9?= | last post by:
Hi, I was trying to find a way to set, upon __init__() the parent of a class to an existing instance. Here is a minimal example of what I'm trying to do: class A(object): def __init__(self, x):...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.