Question about using copy constructor of parent class?

F

flamexx7

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(){
}
 
R

Rolf Magnus

flamexx7 said:
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) {}
 
F

flamexx7

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();
}
 
J

joosteto

flamexx7 said:
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?
 
R

Rolf Magnus

flamexx7 said:
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.
 
F

flamexx7

flamexx7 said:
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){
 

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
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top