考虑以下代码段:
class Window // Base class for C++ virtual function example
{
public:
virtual void Create() // virtual function for C++ virtual function example
{
cout <<"Base class Window"<<endl;
}
};
class CommandButton : public Window
{
public:
void Create()
{
cout<<"Derived class Command Button - Overridden C++ virtual function"<<endl;
}
};
int main()
{
Window *button = new CommandButton;
Window& aRef = *button;
aRef.Create(); // Output: Derived class Command Button - Overridden C++ virtual function
Window bRef=*button;
bRef.Create(); // Output: Base class Window
return 0;
}
aRef和bRef都被分配*button,但为什么两个输出不同。分配给引用类型和非引用类型有什么区别?