引用传递的话,对于virtual函数可以正确调用正确的函数
class Base{
public:
virtual void print() const
{
cout<<"Base"<<endl;
}
};
class Child : public Base{
public:
void print() const override
{
cout<<"Child"<<endl;
}
};
void Print1(const Base &base)
{
base.print();
}
void Print2(const Base base)
{
base.print();
}
int main()
{
Child child;
Print1(child);
Print2(child);
return 0;
}