使用赋值运算符 [英] using assignment operator

查看:102
本文介绍了使用赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Experts!


假设我有一个名为SphereClass的类作为基类,而一个名为BallClass的类是从SphereClass派生的。复制构造函数

初始化左侧对象,在这种情况下对象b2如下。它也是
从SphereClass类初始化子对象。


BallClass复制构造函数看起来像这样。

BallClass :: BallClass(const BallClass& bc):SphereClass(bc)


int main()

{

BallClass b1(" ball") ;


BallClass b2(b1);

返回0;

}


现在问我使用赋值时的问题

b2 = b1

如何从SphereClass类初始化子对象部分。当我使用

复制构造函数时,我使用这个

语句来初始化子对象部分SphereClass(bc)


非常感谢!


// Tony

Hello Experts!

Assume I have a class called SphereClass as the base class and a class
called BallClass that is derived from the SphereClass. The copy constructor
initialize the left hand object in this case object b2 below. It also
initialize subobject from class SphereClass.

The BallClass copy constructor looks like this.
BallClass::BallClass(const BallClass& bc) : SphereClass(bc)

int main()
{
BallClass b1("ball");

BallClass b2(b1);
return 0;
}

Now to my question when you use assignment for example
b2 = b1
how do you initialize the subobject part from class SphereClass. When I used
the copy constructor I initialized the subobject part by using this
statement SphereClass(bc)

Many thanks!

//Tony

推荐答案

Tony Johansson写道:
Tony Johansson wrote:
假设我有一个名为SphereClass的类作为基类,还有一个名为BallClass的类,派生自SphereClass。复制构造函数
在这种情况下初始化左手对象b2下面的对象。它还从SphereClass类初始化子对象。

BallClass复制构造函数看起来像这样。
BallClass :: BallClass(const BallClass& bc):SphereClass(bc)

int main()
{BallClass b1(ball);

BallClass b2(b1);
返回0;
}

现在问我使用赋值时的问题
b2 = b1
如何从SphereClass类初始化子对象部分。


使用作业时,没有_initialised_。一切都是

_assigned_。你最喜欢的C ++书给出了什么样的复制赋值运算符?在那个例子中你有什么不明白的?


顺便说一句,你不喜欢编译器生成的副本分配

运算符?它是不是因为某些原因不适合你?

当我使用
复制构造函数时,我使用这个
语句初始化子对象部分SphereClass(bc)
Assume I have a class called SphereClass as the base class and a class
called BallClass that is derived from the SphereClass. The copy constructor
initialize the left hand object in this case object b2 below. It also
initialize subobject from class SphereClass.

The BallClass copy constructor looks like this.
BallClass::BallClass(const BallClass& bc) : SphereClass(bc)

int main()
{
BallClass b1("ball");

BallClass b2(b1);
return 0;
}

Now to my question when you use assignment for example
b2 = b1
how do you initialize the subobject part from class SphereClass.
When you use assignment, nothing is _initialised_. Everything is
_assigned_. What example of the copy assignment operator does your
favourite C++ book give? What do you not understand in that example?

BTW, what do you not like about the compiler-generated copy assignment
operator? Does it not work for you for some reason?
When I used
the copy constructor I initialized the subobject part by using this
statement SphereClass(bc)




我们注意到了。初始化列表是_constructors_only_的一个特性。


V



We noticed. Initialiser lists are a feature of _constructors_only_.

V


Tony,您的基类SphereClass是否有复制构造函数?如果是这样,

然后你的成员初始化语句SphereClass(bc)没问题。

否则如果您的基类没有复制构造函数,默认

成员初始化以递归方式应用于基类。

Tony, Does your base class SphereClass have a copy constructor? If so,
then your memberwise initialization statement SphereClass(bc) is okay.
Otherwise if your base class does not have a copy constructor, default
memberwise initialization is recursively applied to the base class.


Victor,编译器生成的复制赋值运算符并不总是

最好的选择。 Tony没有向我们提供他的

级别的完整描述。如果他的基类包含任何指向动态分配内存的
指针的成员,我就不会使用编译器生成的

复制赋值运算符。相反,我会像Tony一样写自己的建议。

Victor, Compiler-generated copy assignment operators are not always the
best choice. Tony has not provided us with a full description of his
class. If his base class contains any members which are pointers to
dynamically allocated memory, I would not use the compiler-generated
copy assignment operators. Instead, I would write my own as Tony
suggests.


这篇关于使用赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆