想不通,求指教
先上代码吧,不相干的省略
#include <iostream> #include <typeinfo> using namespace std; class Fly{ public: //virtual void fly()=0; }; class Bird:public Fly{ public: /*virtual void fly(){ cout<<"i am a bird,i can fly"<<endl; }*/ }; class Plane:public Fly{ public: /*virtual void fly(){ cout<<"i am a plane,i can fly!"<<endl; }*/ }; void doSomething(Fly *f){ cout<<"i am "<<typeid(*f).name()<<endl; } int main(){ Fly *f=new Plane(); doSomething(f); delete f; f=NULL; Bird b; doSomething(&b); return 0; }
打印结果是
i am 3Fly
i am 3Fly
而把类底下的注释取消,打印结果是
i am 5Plane
i am 4Bird
这里的3 4 5是什么 怎么来的?