| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu Nov 09, 2006 10:11 am Post subject: define a copy constructor in a class having data member as a |
|
|
Hi,
can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}
};
class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??
};
Any idea ...Thanks |
|
| Back to top |
|
 |
Greg Guest
|
Posted: Thu Nov 09, 2006 10:11 am Post subject: Re: define a copy constructor in a class having data member |
|
|
dalu.gelu (AT) gmail (DOT) com wrote:
| Quote: | can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}
};
class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??
};
|
Like so:
class B
{
public:
B( B& b1 )
: a1( b1.a1 )
{
}
};
Greg |
|
| Back to top |
|
 |
Greg Guest
|
Posted: Thu Nov 09, 2006 10:11 am Post subject: Re: define a copy constructor in a class having data member |
|
|
dalu.gelu (AT) gmail (DOT) com wrote:
| Quote: | Hi,
can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}
};
class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??
};
|
Like so:
class B
{
A a1;
public:
B( B& b1 ) : a1( b1.a1 )
{ }
};
Greg |
|
| Back to top |
|
 |
VJ Guest
|
Posted: Thu Nov 09, 2006 10:11 am Post subject: Re: define a copy constructor in a class having data member |
|
|
dalu.gelu (AT) gmail (DOT) com wrote:
| Quote: | Hi,
can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}
};
class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??
};
Any idea ...Thanks
|
1) add a public method to class A to assign a value to x
2) make class B friend of class A |
|
| Back to top |
|
 |
|