niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
Question about using copy constructor of parent class?
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
Author Message
LJB
*nix forums beginner


Joined: 24 Jan 2006
Posts: 9

PostPosted: Thu Jul 20, 2006 9:32 pm    Post subject: Question about using copy constructor of parent class? Reply with quote

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

PostPosted: Thu Jul 20, 2006 9:54 pm    Post subject: Re: Question about using copy constructor of parent class? Reply with quote

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


Quote:
};
int main(){
}
Back to top
LJB
*nix forums beginner


Joined: 24 Jan 2006
Posts: 9

PostPosted: Fri Jul 21, 2006 9:22 am    Post subject: Re: Question about using copy constructor of parent class? Reply with 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();
}




"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

PostPosted: Fri Jul 21, 2006 9:32 am    Post subject: Re: Question about using copy constructor of parent class? Reply with quote

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

PostPosted: Fri Jul 21, 2006 9:57 am    Post subject: Re: Question about using copy constructor of parent class? Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 2:35 pm | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Newbie question: How to forward a domain to a mailbox? leei Postfix 0 Fri Aug 24, 2007 4:55 pm
No new posts configuration question for httpd Karl Wang Apache 1 Fri Jul 21, 2006 2:10 pm
No new posts nim problem/question Ron AIX 0 Fri Jul 21, 2006 1:57 pm
No new posts question for JAVA developer who r using postgres sql as b... deepak pal PostgreSQL 1 Fri Jul 21, 2006 9:00 am
No new posts class that have a member of type that's derived from it Mirko Puhic C++ 8 Fri Jul 21, 2006 1:53 am

Debt Consolidation | Personal Loans | Electricity Suppliers | Credit Card Consolidation | Mortgage Calculator
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2213s ][ Queries: 16 (0.1292s) ][ GZIP on - Debug on ]