gpt4 book ai didi

c++ - 我可以使用 = 运算符将一个对象的值分配给另一个对象,而不重载运算符吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:22 26 4
gpt4 key购买 nike

这是最近在我的计算机类(class)中出现的一个 T/F 问题,我发现它有点令人困惑。

教科书上说:

The = operator may be used to assign one object's data to another object, or to initialize one object with another object's data. By default, each member of one object is copied to its counterpart in the other object.

问题逐字是:

You cannot use the = operator to assign one object's values to another object, unless you overload the operator. T/F?

从教科书的那一段来看,我回答错了。然而,事实证明测验答案是真的。

当我在网上查找问题时,我看到其他来源也将答案列为“错误”。诚然,这些只是通用的抽认卡/测验网站,所以我并没有在其中放太多股票。

基本上,我只是好奇真正的答案是什么以供将来学习之用。


P.S.:教科书后面接着说:“为了改变赋值运算符的工作方式,它必须重载。运算符重载允许您在与类一起使用时重新定义现有运算符的行为对象。”

我觉得这是相关的并支持“真实”答案,但我不太确定。

最佳答案

声明

You cannot use the = operator to assign one object's values to another object, unless you overload the operator.

... 是微不足道的错误,因为您可以分配给例如一个 int 变量。这是一个对象。在 C++ 中。

可能他们打算写“类类型对象”,但即便如此它也是错误的。例如。任何 POD(普通旧数据)类类型对象都是可赋值的,并且缺少重载的赋值运算符。

但是,有些情况下您希望或需要禁止或负责分配。

关于c++ - 我可以使用 = 运算符将一个对象的值分配给另一个对象,而不重载运算符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39762111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com