C++ 为什么可以';我能在MVSE 2012中使用为相同情况定义的转换构造函数和转换运算符编译代码吗?

C++ 为什么可以';我能在MVSE 2012中使用为相同情况定义的转换构造函数和转换运算符编译代码吗?,c++,visual-studio-2012,visual-c++,gcc,visual-studio-express,C++,Visual Studio 2012,Visual C++,Gcc,Visual Studio Express,我正在学习转换构造函数和cast运算符的显式关键字,我不得不问: 当用户定义类型之间的隐式转换同时为其编写了转换构造函数和强制转换运算符时,会发生什么情况? 看起来转换规则真的很复杂。 我尝试了以下代码: class Type2; class Type1 { public: Type1 (int num) { number1 = num; } Type1 (Type2&); int number1; }; class Type2 { public: Type2 (int nu

我正在学习转换构造函数和cast运算符的显式关键字,我不得不问: 当用户定义类型之间的隐式转换同时为其编写了转换构造函数和强制转换运算符时,会发生什么情况? 看起来转换规则真的很复杂。 我尝试了以下代码:

class Type2;

class Type1 {
public:
Type1 (int num) {
  number1 = num;
}
Type1 (Type2&);
  int number1;
};

class Type2 {
public:
Type2 (int num) {
  number2 = num;
}
Type2 (Type1&);
operator Type1() const;
  int number2;
};

Type1::Type1 (Type2& arg) {
  number1 = arg.number2;
}

Type2::Type2 (Type1& arg) {
  number2 = arg.number1;
}

Type2::operator Type1() const {
  Type1 cast = Type1(500);
  return cast;
}

void testFunction(Type1 contention){
  cout << contention.number1 << endl;
}

int main()
{
  Type2 toBeConverted = Type2(5);
  testFunction(toBeConverted);
}
class-Type2;
类别1{
公众:
类型1(整数){
number1=num;
}
类型1(类型2&);
整数1;
};
类别2{
公众:
类型2(整数){
number2=num;
}
类型2(类型1&);
运算符Type1()常量;
整数2;
};
类型1::类型1(类型2和参数){
number1=arg.number2;
}
类型2::类型2(类型1和参数){
number2=arg.number1;
}
类型2::运算符类型1()常量{
类型1铸件=类型1(500);
回浇;
}
void testFunction(类型1争用){

CUT Visual C++ 2015编译代码干净。哈,真的吗?为什么?我仍然觉得奇怪,这可能只是其中一个原因,因为它们没有完全实现标准的原因,所以遵循标准这里是不可移植的。我用Visual C++ 2012复制失败。(这是编译器报告的17.00.50727.1版)。是的,我有17.00.50727.42…但如果我能洞察其中的原因,那就太好了