为什么下面的C++程序输出“ABaBbAc”?C++运算符重载和参数化构造函数
#include "stdafx.h"
#include <iostream>
using namespace std;
class A {
public:
int i;
A(int j=0):i(j)
{
cout<<"A";
}
operator int()
{
cout<<"a";
return 2;
}
};
class B {
public:
B(int j=1):i(j){
cout<<"B";
}
operator int() {
cout<<"b";
return 3;
}
int i;
};
int operator+(const A&a, const B&b){
cout<<"C";
return a.i + b.i;
}
int main()
{
A a;
B b;
int i = (A)b + (B)a;
return 0;
}
你会期望它做什么? – Beta
这是一个编程面试问题,我无法理解.. – RDX
字符串中的小写字母a和b表示A和B类型的相应对象resp被转换为某个int值 – damienh