可以安全地使用编译器生成的赋值运算符吗?

发布于 2024-07-09 21:32:28 字数 325 浏览 6 评论 0原文

我正在使用 MFC 中的 CPoint 类。 没有明确定义的赋值运算符或复制构造函数(AFAIK)。 然而,这是有效的:

CPoint p1(1, 2), p2;
p2 = p1; // p2 now is equal to p1

我假设这是由于编译器生成的赋值运算符而自动工作的。 正确的?

如果是这样,我可以确信这不会造成任何意外吗? 在这种情况下,CPoint 非常简单,我认为一切都很好,但总的来说,这让我有点担心。 这是更好的形式吗:

p2.SetPoint(p1.x, p2.x);

-cr

I'm using the CPoint class from MFC. There is no explicitly defined assignment operator or copy constructor (AFAIK). Yet, this works:

CPoint p1(1, 2), p2;
p2 = p1; // p2 now is equal to p1

I'm assuming this is working automagically because of a compiler generated assignment operator. Correct?

If so, can I be confident that this isn't doing anything unexpected? In this case CPoint is so simple I think all is well, but in general this is something that worries me a bit. Is it better form to do:

p2.SetPoint(p1.x, p2.x);

-cr

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

寂寞花火° 2024-07-16 21:32:28

这是安全的 - 如果不打算提供赋值运算符,那么 MFC 设计者可以确保它不可用(例如,通过将其设为私有)。

IIRC 编译器将执行逐个成员的复制,因此对于包含这样的 POD 的类,不会有问题。 如果您有一个分配内存的类并且忽略覆盖operator=并执行深度复制,那么它可能会变得混乱。

FWIW 我问了一个关于编译器可以做什么和不能做什么的问题:

为什么 C++ 编译器不定义operator== 和operator!=?

有些答案读起来很有趣。

This is safe - if an assignment operator wasn't meant to be supplied then the MFC designers could have made sure it wasn't available (by making it private for example).

IIRC the compiler will perform a member-by-member copy, so for a class containing POD like this, you won't have a problem. It can get messy if you have a class that allocates memory and neglects to override operator= and perform a deep-copy.

FWIW I asked a question about what the compiler can and cannot do a while back:

Why don't C++ compilers define operator== and operator!=?

Some of the answers make for interesting reading.

无法言说的痛 2024-07-16 21:32:28

查找默认复制构造函数:

http://www.fredosaurus.com/ Notes-cpp/oop-condestructors/copyconstructors.html

这并不是 CPoint 的特殊之处。

Look up default copy constructor:

http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html

This isn't a special thing about CPoint.

深巷少女 2024-07-16 21:32:28

如果一个类是“简单的”,那么编译器生成的赋值运算符将起作用(按成员复制)。 如果有一些成员需要更高级的逻辑(假设该对象维护一个指向它所期望的私有缓冲区的内部指针),那么编译器生成的赋值运算符将会出现问题。 CPoint 仅存储点的 x 和 y 坐标,因此您不应该遇到问题。

If a class is "simple", then the compiler-generated assignment operator will work (memberwise copy). If there are some members that would require more advanced logic (let's say the object maintains an internal pointer to what it expects is a private buffer), then the compiler-generated assignment operator will have issues. CPoint just stores an x and y coordinate of a point, so you should not run into issues.

归属感 2024-07-16 21:32:28

对于像 CPoint 这样的简单数据对象(您可以从 Visual Studio 安装中包含的 MFC 源代码中看到,它只是一个附加了一些便利函数的 Win32 POINT 结构),使用编译器生成的赋值运算符没有任何问题。

但正如已经提到的,默认的赋值运算符是浅拷贝,如果结构包含指针(或包含包含指针但未定义赋值运算符的结构),则会给您带来麻烦。 由于 CPoint 不符合该描述,因此它是安全的。

For a simple data object like CPoint (which you can see from the MFC source included with your Visual Studio installation is just a Win32 POINT structure with a few convenience functions tacked on), there is nothing wrong with using the compiler-generated assignment operator.

But as already mentioned, the default assignment operator is a shallow copy and will get you into trouble if the structure contains pointers (or contains structures that contain pointers without defining an assignment operator). Since CPoint doesn't fit that description, it's safe.

用心笑 2024-07-16 21:32:28

内置的复制赋值运算符只是使用其复制赋值运算符依次复制每个成员。 我认为,在不查看其文档的情况下,CPoint 是安全的(原因:如果不是,他们当然会提供自己的实现)。 该点类应该只有两个成员(x 和 y),并且它们只是浮点数(或 int 取决于它的用途)。

有人说这是“浅复制”,因为仅复制成员的值。 如果有指针成员,则复制指针值,而不是指针指向的对象。

The builtin copy assignment operator just copies each member in turn using their copy assignment operators. I assume that it's safe for CPoint without looking at its documentation (reason: If it wasn't they would provide an own implementation, of course). That point class should just have two members (x and y) and those are just floats (or int depending for what it's used).

One says it's a "shallow copy" since only the values of the members are copied. If you have pointer members, the pointer values are copied, instead of the objects where the pointers point to.

拿命拼未来 2024-07-16 21:32:28

我不了解 MFC,但我可以猜测(或者):

  • CPoint 有一个定义的赋值运算符(因此不是编译器生成的),或者
  • 编译器生成的赋值运算符在成员进行堆栈分配时起作用,因此它会进行成员复制。

I don't know MFC, but I can guess that (alternatively):

  • CPoint has a defined assignment operator (so not compiler-generated), or
  • Compiler-generated assignment operator works when members are stack-allocated, so it does memberwise copy.
玩物 2024-07-16 21:32:28

是的。 如果您没有在类上定义 operator= 方法,编译器会为您生成一个方法,该方法仅对类中的字段进行按位复制。 我记得,CPoint 只是 {int x; int y},所以按位复制就可以了。

Yes. If you define no operator= method on a class, the compiler generates one for you that simply does a bitwise copy of the fields in the class. As I recall, CPoint is merely {int x; int y}, and so bitwise copy is fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文