C++中類的內(nèi)存空間大?。╯izeof)分析
#include class test class derived_test:public test class base int base::get_count() int base::refrence_count = 0; class new_base int new_base::get_count() class derived: public base class new_derived:public new_base int main() 上面在windows和linux的結(jié)果分別如下:
#include
#include
{
public:
test();
virtual ~test();
virtual void get_a_c();
private:
int a;
char c;
};
{
public:
virtual ~derived_test();
private:
doubled ;
};
{
private:
char a;
static int refrence_count;
std::string name;
double price;
std::vector
public:
base();
virtual ~base();
static int get_count();
};
{
return refrence_count;
}
{
private:
char a;
double price;
std::vector
std::string name;
static int refrence_count;
public:
new_base();
virtual ~new_base();
static int get_count();
};
{
return refrence_count;
}
int new_base::refrence_count = 0;
{
private:
int min_qty;
double discount;
static int newp;
public:
derived();
virtual ~derived(){};
};
{
private:
doublediscount;
int min_pty;
static int newp;
public:
new_derived();
virtual ~new_derived(){}
};
{
std::cout << "The size of test is " << sizeof(test) << std::endl;
std::cout << "The size of derived_test is " << sizeof(derived_test) << std::endl;
std::cout << "The size of base is " << sizeof(base) << std::endl;
std::cout << "The size of new_baseis " << sizeof(new_base) << std::endl;
std::cout << "The size of derived is " << sizeof(derived) << std::endl;
std::cout << "The size of new_derived is " << sizeof(new_derived) << std::endl;
return 0;
}
windows:
Linux:
從上面的結(jié)果可以之知道在兩個系統(tǒng)下,結(jié)果是不一樣的。說明操作系統(tǒng)也對類的存儲空間大小有較大的影響。
評論