在具有数据成员作为另一个类的对象的类中定义复制构造函数 [英] define a copy constructor in a class having data member as an object of another class
问题描述
任何人都可以通过编写一个定义副本的示例代码来帮助我
构造函数在一个具有数据成员作为另一个对象的类中
class。
例如:
A级{
int x;
public:A (){x = 6;}
};
B级{
A a1;
public:B(B& b1)
{???} //我如何分配数据成员?
};
任何想法......谢谢
da ******* @ gmail.com 写道:
任何人都可以帮我写一个定义副本的示例代码
构造函数在一个类中,数据成员作为另一个
类的对象。
例如:
A级{
int x;
public:A(){x = 6;}
};
B级{
A a1;
public:B(B& b1 )
{ ???} //我如何分配数据成员??
};
任何想法......谢谢
1)将一个公共方法添加到A类以赋值给x
2)制作A类的B级朋友
< br>
da*******@gmail.com 写道:
< blockquote class =post_quotes>
任何人都可以通过编写一个定义副本的示例代码来帮助我
构造函数在一个类中,数据成员作为另一个对象的对象
class。
例如:
A类{
int x;
public:A() {x = 6;}
};
B级{
A a1;
public:B(B& b1)
{???} //我如何分配数据成员?
};
喜欢这样:
B级
{
公开:
B(B& b1)
:a1(b1.a1)
{
} < br $>
};
Greg
da ******* @ gmail.com 写道:
任何人都可以通过编写一个定义副本的示例代码来帮助我。
构造函数在一个类中,数据成员是另一个
类的对象。 br />
例如:
A类{
int x;
public:A(){x = 6; }
};
B级{
A a1;
公开:B(B & b1)
{???} //我如何分配数据成员?
};
喜欢这样:
B级
{
A1;
公开:
B(B& b1):a1(b1.a1)
{}
};
格雷格
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
da*******@gmail.com wrote: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
da*******@gmail.com wrote: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
da*******@gmail.com wrote: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
这篇关于在具有数据成员作为另一个类的对象的类中定义复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!