重载运算符.Const论点会产生一些问题
overloading operator. Const arguments make some problems
class Vector{
......
.......
private:
int dim;
public:
int getDim() {
return this->dim;
}
const Vector operator+(const Vector& right){
this->getSize();
}
};
我在这个->getSize((;中得到了编译错误;。它是由事实引起的,那个论点是正确的。我不知道哪里出了问题。我不想修改对。
假设您有一个非常量方法Vector::getSize()
。您需要将其设为const
,以便可以在const
对象上或通过const
引用或指向const
的指针调用它。例如:
int getSize() const;
^^^^^
还要注意,返回const
值没有多大意义(如果有移动语义,则会禁止它们(。加法成员运算符的规范形式是
// const method: A = B + C should not modify B
Vector operator+(const Vector& right) const;
^^^^^
和非成员
Vector operator+(const Vector& left, const Vector& right);
相关文章:
- 一个关于在C++中重载布尔运算符的问题
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- 运算符继承和 cpp 核心准则 c.128 的问题
- 在C++中使用重载提取运算符时出现问题
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- 关于条件块的问题与&&运算符有关
- 重载运算符与添加问题
- 加、乘、除、减复数的问题 C++ - 运算符重载
- 我需要这方面的帮助C++在使用逻辑运算符时是/否问题
- 0xC0000005:访问冲突读取位置 0x00000000. 重载 == 运算符的问题
- 如何解决不明确的运算符过载问题?
- 屏幕插入运算符<<的运算符过载问题
- 如何通过使用 2 位或更多数字的 XOR 运算符来执行此操作C++问题
- 在 C++ 的自定义运算符中删除与删除[](不同于常见的删除与删除[]问题)
- 用户定义的文本运算符(在原始模式下)存在问题
- 关于重载 -> 运算符中 const 关键字的特定位置的问题
- 字符 * 未从重载运算符或内存管理问题正确返回
- constexpr 运算符重载使用参数的问题
- c++中重载输入运算符的问题
- 有趣的问题运算符重载