我编写了一个不可变的类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'分配了内存,所以当我想用其他东西替换那个内存中的东西时,它不会像那样 . 我对么?我该如何解决这个问题?
谢谢!
1 回答
你不希望'next'成为Coords的引用 . 你希望它是Coords类型的对象 . 试试这个:
当'next'超出范围时(通常在方法结束时),运行时将调用'next'的析构函数
您将希望在任何情况下编写(加运算符)覆盖 . (你的对象不是一成不变的 . 你需要阅读它) .