O
Osamede.Zhang
Compiler store a pointer table in each object with virtual fuction to
implement polymorphic,isn't it? But where is the pointer table?
It seems that object only store a pointer to the table,the output of
this code is 88.
#include<iostream>
using namespace std;
class MyClass{
public:
MyClass(){ a=0;}
virtual void tst1() {a-=1;}
virtual void tst2(){a*=2;}
virtual void tst3(){a*=1;}
private:
int a;
};
class Tstublic MyClass{
void tst1() {std::cout<<"Tst"<<endl;}
void tst2(){std::cout<<"Tst"<<endl;}
void tst3(){std::cout<<"Tst"<<endl;}
};
int main()
{
//Tst tstObject;
//MyClass *tstp=&tstObject;
//tstp->sub();
Tst tstObject;
MyClass myObject;
cout<<sizeof(myObject);
cout<<sizeof(tstObject);
return 1;
}
implement polymorphic,isn't it? But where is the pointer table?
It seems that object only store a pointer to the table,the output of
this code is 88.
#include<iostream>
using namespace std;
class MyClass{
public:
MyClass(){ a=0;}
virtual void tst1() {a-=1;}
virtual void tst2(){a*=2;}
virtual void tst3(){a*=1;}
private:
int a;
};
class Tstublic MyClass{
void tst1() {std::cout<<"Tst"<<endl;}
void tst2(){std::cout<<"Tst"<<endl;}
void tst3(){std::cout<<"Tst"<<endl;}
};
int main()
{
//Tst tstObject;
//MyClass *tstp=&tstObject;
//tstp->sub();
Tst tstObject;
MyClass myObject;
cout<<sizeof(myObject);
cout<<sizeof(tstObject);
return 1;
}