GCC编译器下才会有的有关问题,构造函数冲突了,不理解是为什么
GCC编译器下才会有的问题,构造函数冲突了,不理解是为什么
#include <iostream>
class MyInt{
public:
MyInt() {
std::cout << "1" << std::endl;
}
MyInt(int a) {
std::cout << "2" << std::endl;
}
void operator =(int a) {
std::cout << "3" << std::endl;
}
MyInt(MyInt &b) {
std::cout << "4" << std::endl;
}
};
int main() {
MyInt a;
MyInt b = 10; //和myInt(MyInt &b) 产生冲突
std::cout << "-------------" << std::endl;
MyInt c;
c = 10;
std::cout << "-------------" << std::endl;
MyInt d;
MyInt e = d;
}
------解决方案--------------------
因为 const MyInt& 可以绑定到临时对象上,然后就没有编译错误了。
#include <iostream>
class MyInt{
public:
MyInt() {
std::cout << "1" << std::endl;
}
MyInt(int a) {
std::cout << "2" << std::endl;
}
void operator =(int a) {
std::cout << "3" << std::endl;
}
MyInt(MyInt &b) {
std::cout << "4" << std::endl;
}
};
int main() {
MyInt a;
MyInt b = 10; //和myInt(MyInt &b) 产生冲突
std::cout << "-------------" << std::endl;
MyInt c;
c = 10;
std::cout << "-------------" << std::endl;
MyInt d;
MyInt e = d;
}
------解决方案--------------------
因为 const MyInt& 可以绑定到临时对象上,然后就没有编译错误了。