C++ - 多态实现原理

2016-01-27  本文已影响1094人  Mitchell

多态的实现举例

class Base{
    public:
      int i;
      virtual void Print(){out<<"Base:Print";}
};
class Derived:public Base{
      public:
      int n;
      virtual void Print(){out<<"Derived:Print"<<endl;}
}
int main(){
      Derived d;
      cout<<sieof(Base)<<","<<sizeof(Derived);
      return 0 ;
}

输出结果:8,12
比正常输出多出了4个字节。


多态实现原理


多态的关键 -- 虚函数表


多态存在的问题

上一篇下一篇

猜你喜欢

热点阅读