为什么这是打印"operator + operator +"输出?有人可以对此有所了解吗?
Why this is printing "operator + operator +" as out put? Could somebody please shed some light on this?
为什么要将"operator+operator+"打印为输出?我的期望是"运算符+双运算符+"。有人能解释一下吗?
#include<iostream>
using namespace std;
struct mydata{
int mx;
mydata(int x = 0){}
mydata operator+(const mydata& rhs){
cout<<" operator + ";
mydata temp(rhs);
return temp;
}
operator int() const{cout<<" int "; return mx; }
operator double() const{cout<" double "; return mx; }
};
int main(){
mydata d;
mydata r = d + mydata(5); // L1
5 + (double)d; // L2
d + d; // L3
}
cout<" double "
应为cout << " double "
。我很惊讶能按原样编译。
奇怪的是,它与VS2008
一起编译和运行,并产生OP的答案和这个容易错过的警告:
warning C4552: '<' : operator has no effect; expected operator with side-effect
然而,GCC在编译时确实会发出砰的一声(这实际上很好):
test.cpp: In member function 'mydata::operator double() const':
test.cpp:14: error: no match for 'operator<' in 'std::cout < " double "'
test.cpp:14: note: candidates are: operator<(const char*, const char*) <built-in>
test.cpp:14: note: operator<(void*, void*) <built-in>
因此,如果OP正在使用VS
,那么关注警告可能是件好事。
相关文章:
- 正在尝试了解输入验证循环
- 了解 GLM- openGL 中的相机转换
- C++我需要了解在哪里使用指针和双指针
- 如何深入了解明显的腐败
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- 'operator='已弃用:改用 QDir::setPath()
- 了解嵌套循环打印星号图案
- 过载'operator new'如何导致无限循环?
- 了解每月第一天函数的代码
- 了解C++标准::shared_ptr
- 尝试了解在导入的静态方法上使用删除方法时的错误
- C++:需要帮助了解运算符重载错误
- 与'operator='不匹配(操作数类型'String'且"void")
- 在学习数据结构之前对STL有一个了解是好的吗?
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 了解算法的性能差异(如果以不同的编程语言实现)
- 了解删除C++
- 处理"no operator found"
- 为什么替换malloc()需要了解链接器,而::operator new()不需要
- 为什么这是打印"operator + operator +"输出?有人可以对此有所了解吗?