如果方法应该返回B
类型的对象,为什么它返回int
的方法B minus
?它为什么会在返回一个对象的方法中返回一个int?
#include <iostream>
class B
{
public:
int a;
public:
B(int i=0)
{
a=i;
}
B minus()
{
return (1-a);
}
};
int main()
{
B x(18);
x = x.minus();
std::cout << x.a << '\n';
return 0;
}
你没有标注你的构造'explicit'。 – user2357112
看看你的'B'构造函数。 “B”如何构建? – PaulMcKenzie
我假设由于构造函数采用int,所以它也是从int中隐式构造的。 –