不懂:求助基类类型指针指向派生类对象?
#include <iostream.h>
class Base
{
public:
Base() { }
virtual ~Base()
{
cout<<" call Base::~Base "<<endl;
}
};
class Derived: public Base
{
public:
Derived() { };
~Derived()
{
cout<<" call Derived::~Derived "<<endl;
}
};
main()
{
Base *pob_b =new Derived;
delete pob_b;
return 0;
}
红色部分是基类类型指针指向派生类对象,我的疑问是:指针不是都有类型限制吗?
Base 型指针怎么能够 “随便” 指向一个其他类的对象(这里就是派生类对象)呢?为什么不出错呢?疑惑ing
请教!