473,800 Members | 3,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2315
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(tem p){}
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(tem p){}
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@nonewro te in message
news:44******** *************** @news.sunsite.d k...
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
8066
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
4323
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, a method (event) needs to be able to invoke methods of the second class. With static classes its possible but this is not desirable. There's obviouly some visibility problem I am not familiar with. It is not a parent-child relationship since...
2
3032
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 I think my problem has to do with my lack of understandign of some issues in C++. There is a class in QWT called QwtPicker which allows one to make rubberbands and select part of the drawing canvas, very useful in zooming etc. Function...
3
1604
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 child class, one by the parent class. I can do this: class Child { public: Child( int parm1, int parm2, int parm3) : Parent( parm3) {
13
2480
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 certain code syntax but more a 'code architecture' thing , I'll use simple example classes (which are certainly not complete or working...) just to illustrate the idea (and I may make some mistakes because I'm not that experienced...). The...
61
2986
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; function my_baseclass(){ $this->TableName = "";
5
1551
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
1320
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) { cout <<"ctest copy constr" << endl; }; ctest(int a) { cout <<"ctest int constor" <<endl; };
1
2068
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): self.x = x class B(A): def __init__(self, *args):
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10269
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10032
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.