为什么基类对象必须是调用派生虚函数的引用?
#include<iostream>
using namespace std;
class A {
public:
virtual void print() { cout << "Hello 1" << endl; }
};
class B : public A {
public:
int x;
void print() { cout << "Hello " << x << endl; }
};
void main(){
B obj1;
A &obj2 = obj1;
A obj3 = obj1; // Why it is different from obj2
obj1.x = 2;
obj1.print();
obj2.print();
obj3.print(); // ?
}