【发布时间】:2013-06-12 16:45:10
【问题描述】:
为什么下面的 C++ 程序会输出“ABaBbAc”?
#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;
}
【问题讨论】:
-
你希望它做什么?
-
这是一道编程面试题,我看不懂..
-
字符串中的小写字母a和b分别表示A和B类型的对应对象分别转换为某个int值
标签: c++ constructor operator-overloading