<分区>
0我对 C++ 很生疏。我尝试了各种搜索,但找不到答案。我有以下代码:
class Base
{
public:
Base() {};
virtual void hello() { std::cout << "I'm the base class" << std::endl; }
};
class Alice : public Base
{
public:
Alice() {};
virtual void hello() { std::cout << "I'm Alice" << std::endl; }
};
class Bob : public Base
{
public:
Bob() {};
virtual void hello() { std::cout << "I'm Bob" << std::endl; }
};
Base test(int alice)
{
if(alice)
return Alice();
else
return Bob();
}
int main()
{
Base a = test(0);
Base b = test(1);
a.hello();
b.hello();
}
我希望输出是
I'm Alice
I'm Bob
反而是
I'm the base class
I'm the base class
这里发生了什么(除了 Python-on-the-brain)?