类赋值运算符
我进行了以下运算符重载测试:
#include <iostream>
#include <string>
using namespace std;
class TestClass
{
string ClassName;
public:
TestClass(string Name)
{
ClassName = Name;
cout << ClassName << " constructed." << endl;
}
~TestClass()
{
cout << ClassName << " destructed." << endl;
}
void operator=(TestClass Other)
{
cout << ClassName << " in operator=" << endl;
cout << "The address of the other class is " << &Other << "." << endl;
}
};
int main()
{
TestClass FirstInstance("FirstInstance");
TestClass SecondInstance("SecondInstance");
FirstInstance = SecondInstance;
SecondInstance = FirstInstance;
return 0;
}
赋值运算符的行为符合预期,输出另一个实例的地址。
现在,我实际上如何从另一个实例分配一些东西?例如,这样的事情:
void operator=(TestClass Other)
{
ClassName = Other.ClassName;
}
I made the following operator overloading test:
#include <iostream>
#include <string>
using namespace std;
class TestClass
{
string ClassName;
public:
TestClass(string Name)
{
ClassName = Name;
cout << ClassName << " constructed." << endl;
}
~TestClass()
{
cout << ClassName << " destructed." << endl;
}
void operator=(TestClass Other)
{
cout << ClassName << " in operator=" << endl;
cout << "The address of the other class is " << &Other << "." << endl;
}
};
int main()
{
TestClass FirstInstance("FirstInstance");
TestClass SecondInstance("SecondInstance");
FirstInstance = SecondInstance;
SecondInstance = FirstInstance;
return 0;
}
The assignment operator behaves as-expected, outputting the address of the other instance.
Now, how would I actually assign something from the other instance? For example, something like this:
void operator=(TestClass Other)
{
ClassName = Other.ClassName;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
传统上,赋值运算符和复制构造函数是通过 const 引用定义的,而不是使用按值复制机制。
编辑:我更正了,因为我放置的代码没有返回 TestClass& (参见@sbi的回答)
Traditionnaly the assignment operator and the copy constructor are defined passing a const reference, and not with a copy by value mechanism.
EDIT: I corrected because I had put code that didnt return the TestClass& (c.f. @sbi 's answer)
关于如何从其他班级复制内容,您是正确的。简单的对象可以使用
operator=
来赋值。但是,要警惕
TestClass
包含指针成员的情况 - 如果您只是使用operator=
分配指针,那么两个对象将具有指向同一内存的指针,这可能不是你想要的。相反,您可能需要确保分配一些新内存并将指向的数据复制到其中,以便两个对象都有自己的数据副本。请记住,在为复制的数据分配新块之前,您还需要正确释放分配对象已指向的内存。顺便说一句,您可能应该像这样声明您的
operator=
:这是重载
operator=
时使用的一般约定。 return 语句允许链接赋值(如a = b = c
),并通过const
引用传递参数,避免将Other
复制到函数调用。You are correct about how to copy the contents from the other class. Simple objects can just be assigned using
operator=
.However, be wary of cases where
TestClass
contains pointer members -- if you just assign the pointer usingoperator=
, then both objects will have pointers pointing to the same memory, which may not be what you want. You may instead need to make sure you allocate some new memory and copy the pointed-to data into it so both objects have their own copy of the data. Remember you also need to properly deallocate the memory already pointed to by the assigned-to object before allocating a new block for the copied data.By the way, you should probably declare your
operator=
like this:This is the general convention used when overloading
operator=
. The return statement allows chaining of assignments (likea = b = c
) and passing the parameter byconst
reference avoids copyingOther
on its way into the function call.你所展示的代码就可以做到这一点。但没有人会认为这是一个特别好的实现。
这符合赋值运算符的预期:
顺便说一句,您谈论“其他类”,但您只有一个类,以及该类的多个实例。
The code you've shown would do it. No one would consider it to be a particularly good implementation, though.
This conforms to what is expected of an assignment operator:
BTW, you talk about "other class", but you have only one class, and multiple instances of that class.
赋值运算符的传统规范形式如下所示:(
您也不想调用复制构造函数进行赋值)并且它返回对
*this
的引用。一个幼稚的实现将单独分配每个数据成员:(
请注意,这正是编译器生成的赋值运算符要做的事情,因此重载它是毫无用处的。不过,我认为这是为了练习。)
更好的方法是是采用Copy-And-Swap idiom< /a>。 (如果您发现 GMan 的答案太过压倒性,请尝试我的,这不太详尽。
:)
)请注意,C&S 使用复制构造函数和析构函数进行赋值,因此需要按副本传递对象,正如您在问题中所提到的:The traditional canonical form of the assignment operator looks like this:
(you don't want to invoke the copy constructor for assignment, too) and it returns a reference to
*this
.A naive implementation would assign each data member individually:
(Note that this is exactly what the compiler-generated assignment operator would do, so it's pretty useless to overload it. I take it that this is for exercising, though.)
A better approach would be to employ the Copy-And-Swap idiom. (If you find GMan's answer too overwhelming, try mine, which is less exhaustive.
:)
) Note that C&S employs the copy constructor and destructor to do assignment and therefore requires the object to be passed per copy, as you had in your question:几乎所有人都说了,有一些注意事项:
if (&other != this) // allocate
almost all said, a few notes:
if (&other != this) // assign