| Author |
Message |
LJB *nix forums beginner
Joined: 24 Jan 2006
Posts: 9
|
Posted: Thu Jul 20, 2006 9:32 pm Post subject:
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(){
} |
|
| Back to top |
|
 |
Rolf Magnus *nix forums Guru
Joined: 21 Feb 2005
Posts: 1236
|
Posted: Thu Jul 20, 2006 9:54 pm Post subject:
Re: Question about using copy constructor of parent class?
|
|
|
flamexx7 wrote:
| Quote: | 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.
| Quote: | #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.
| Quote: | };
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) {}
|
|
| Back to top |
|
 |
LJB *nix forums beginner
Joined: 24 Jan 2006
Posts: 9
|
Posted: Fri Jul 21, 2006 9:22 am Post subject:
Re: Question about using copy constructor of parent class?
|
|
|
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" <ramagnus@t-online.de> wrote in message
news:e9ou38$mcu$02$1@news.t-online.com...
| Quote: | 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(){
}
|
|
|
| Back to top |
|
 |
joosteto@gmail.com *nix forums beginner
Joined: 09 Jul 2006
Posts: 20
|
Posted: Fri Jul 21, 2006 9:32 am Post subject:
Re: Question about using copy constructor of parent class?
|
|
|
flamexx7 wrote:
| Quote: | 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? |
|
| Back to top |
|
 |
Rolf Magnus *nix forums Guru
Joined: 21 Feb 2005
Posts: 1236
|
Posted: Fri Jul 21, 2006 9:57 am Post subject:
Re: Question about using copy constructor of parent class?
|
|
|
flamexx7 wrote:
| Quote: | 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.
| Quote: | 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.
| Quote: | };
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. |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|