![](/img/trans.png)
[英]How can I use Move constructor instead of Move assignment operator in my own class Vector?
[英]How Can I Avoid Writing an Assignment Operator for my Immutable Class
我编写了一个不变的类Coords,它只有两个成员-const int x和const int y。 但是,编译器希望我编写一个赋值运算符,据我所知,它对于不可变类型毫无意义。 这是一个例子:
//location is a Coords* and Coords::DOWN is a static const Coords&.
Coords& next = Coords(location);
next = next + Coords::DOWN;
Intellisense不喜欢在第3行中使用“ =”。我认为问题是我已经为“ next”分配了内存,因此当我想用其他内容替换该内存中的内容时,它不会像那样。 我对么? 我该如何解决这个问题?
谢谢!
您不希望“下一个”成为参考坐标。 您希望它成为Coords类型的对象。 尝试这个:
Coords next = *location + Coords::DOWN
当“ next”超出范围时(通常在方法末尾),将在运行时调用“ next”的析构函数。
无论如何,您都想编写一个+(加号运算符)替代。 (您的对象不是一成不变的。您需要对此进行阅读)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.