串联不同的操作符
Catenate different operators
本文关键字:操作符 更新时间:2023-10-16
我正在尝试实现一个支持与不同操作符连接的类:
class MyClass {
public:
template<typename T>
MyClass &operator<<(const T& val ) {
//do something with val
return *this;
}
template<typename T>
MyClass &operator=(const T& val) {
//do something with val
return *this;
}
};
int main() {
MyClass a;
a << "hallo" = 3 << "huuh"; //compiler will complain about
}
我错过什么了吗?
谢谢你的帮助!
由于运算符优先,表达式
a << "hallo" = 3 << "huuh";
被计算为
(a << "hallo") = (3 << "huuh");
,你的编译器抱怨缺乏有效的operator<<(int, const char[5])
。
你需要使用括号来改变优先级:
(a << "hallo" = 3) << "huuh";
也就是说,很难理解这里发生了什么,应该使用操作符来使事情更清晰,而不是更难阅读。
相关文章:
- 为什么map有操作符[],set没有
- 我可以将C 17 Capture lambda ConstexPR转换操作符的结果用作函数指针模板非类型参数吗?
- c++新操作符通过libstdc++占用大量内存(67MB)
- 复制/移动操作符是否可以安全地用于实现复制/移动分配操作符
- 提升::精神::因果报应使用有条件的替代操作符(|)
- 促进几何体操作符
- 如果操作符delete没有实现,为什么不编译它
- 在使用放置新操作符时,我真的需要担心对齐问题吗
- 我怎样才能让getline操作符工作
- 在重载的全局new操作符中使用静态对象会导致核心转储运行时错误
- 提升ipc new和delete操作符
- 在c++中使用new操作符动态分配数组
- 操作符的大小在C++
- 如何将向量中的对象传递给操作符
- 一个更容易的拷贝分配操作符实现
- 如何重写复杂的c++代码行(嵌套的三元操作符)
- boost::make_shared没有调用(放置)操作符new
- 新操作符(以及malloc)无法分配~450MB的内存
- 类、异常和操作符
- 当delete操作符释放内存时,我为什么需要析构函数