#include "stdafx.h"
#include <iostream>
#include <complex>
using
namespace
std;
class
Base
{
public
:
Base(){cout<<
"Base-Ctor"
<<endl;}
~Base(){cout<<
"Base-Dtor"
<<endl;}
virtual
void
f(
int
){cout<<
"Base::f(int)"
<<endl;}
virtual
void
f(
double
){cout<<
"Base::f(double)"
<<endl;}
virtual
void
g(
int
i=10){cout<<
"Base::g()"
<<i<<endl;}
virtual
void
g2(
int
i=10){cout<<
"Base:g2()"
<<i<<endl;}
};
class
Derived:
public
Base
{
public
:
Derived(){cout<<
"Derived-Ctor"
<<endl;}
~Derived(){cout<<
"Derived-Dtor"
<<endl;}
void
f(complex<
double
>){cout<<
"Derived::f(complex)"
<<endl;}
virtual
void
g(
int
i=20){cout<<
"Derived::g()"
<<endl;}
};
int
_tmain(
int
argc, _TCHAR* argv[])
{
Base b;
Derived d;
Base* pb=
new
Derived;
cout<<
sizeof
(Base)<<endl;
cout<<
sizeof
(Derived)<<endl;
return
0;
}
如果类内没有成员变量,只有成员函数,那么有虚函数表时sizeof为4,没有虚函数表为1;
类的大小与它当中的构造函数,析构函数,以及其他的成员函数无关,只与它当中的成员数据还有虚表指针有关