您的位置:程序门 -> c/c++ -> c++ 语言



虚函数占用对象的存储空间?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


虚函数占用对象的存储空间?
发表于:2008-01-23 01:11:20 楼主
#include   <iostream>
using   namespace   std;
class   student
{
public:
void   display();
int   haha;
};

void   student::display()
{
}

class   student1:protected   student
{
public:
int   age;
};

int   main()
{
student   stud;
student1   stud1;
cout < <sizeof(stud1) < <endl;
cout < <sizeof(stud) < <endl;
return   0;
}

输出:
8
4

而将上面红色代码部分改为虚函数virtual   void   display();后,再运行程序,输出结果为:
12
8

为什么会这样呢?是虚函数占用了内存么?但是在第一段程序中再加上一个虚函数,代码如下:
#include   <iostream>
using   namespace   std;
class   student
{
public:
virtual   void   display();
virtual   void   display1();
int   haha;
};

void   student::display()
{
}
void   student::display1()
{
}

class   student1:protected   student
{
public:
int   age;
};

int   main()
{
student   stud;
student1   stud1;
cout < <sizeof(stud1) < <endl;
cout < <sizeof(stud) < <endl;
return   0;
}
运行此程序,结果还是12和8。
为什么呢?
请高手们帮忙解决。
发表于:2008-01-23 01:14:311楼 得分:0
还有一个问题:
#include   <iostream>
using   namespace   std;
class   student
{
public:
  void   display();
};

void   student::display()
{
}
class   student1:protected   student
{
public:
int   age;
};

int   main()
{
student   stud;
student1   stud1;
cout < <sizeof(stud1) < <endl;
cout < <sizeof(stud) < <endl;
return   0;
}

运行此程序,输出结果为
4
1
为什么student对象stud的存储空间是1呢?这1字节内存存放的是什么东西呢?
发表于:2008-01-23 01:14:472楼 得分:0
我没看你的程序,回答不当请见谅。
一般函数是直接调用其入口地址,而虚函数是通过指针来调用的,在类对象中需要保存每个虚函数的指针,相当于每个虚函数在类中对应一个指针变量。所以对于32位程序,每个虚函数占4字节。
发表于:2008-01-23 01:16:373楼 得分:0
类必须要有储存空间,所以对于空类会分配1字节空间。
发表于:2008-01-23 08:25:544楼 得分:0
虚函数不占对象的空间!但是指向虚函数表的指针占4个字节的空间,不论你类里有多少个虚函数,你的对象大小只增加4个字节,你可以试试!
发表于:2008-01-23 08:37:545楼 得分:0
去看《深度探索c++对象模型》吧
发表于:2008-01-23 08:39:506楼 得分:0
一般声名虚函数的时候   编译器会自动在对象里面安插一个指针指向vtable,在32位的机器上面,一个对象会增加4个字节,它是实现面向对象中多态的关键。
发表于:2008-01-23 08:51:317楼 得分:0
楼上几位的答案正点了
哈哈

#include   <cstdlib>
#include   <iostream>
using       namespace       std;  
class       student  
{  
public:  
virtual       void       display();  
virtual       void       display1();  
virtual       void       display2();  
virtual       void       display3();  
virtual       void       display4();  
int       haha;  
};  

void       student::display()  
{  
}  
void       student::display1()  
{  
}  
void       student::display2()  
{  
}  
void       student::display3()  
{  
}  
void       student::display4()  
{  
}  
class       student1:protected       student  
{  
public:  
int       age;  
};  

int       main()  
{  
student       stud;  
student1       stud1;  
cout   < <sizeof(stud1)   < <endl;  
cout   < <sizeof(stud)   < <endl;  
system("pause");
return       0;  
}  
输出仍然是12和8

如yupengchen951124所讲
编译器会自动在对象里面安插一个指针指向vtable,
vtable中存放虚函数的地址
发表于:2008-01-23 09:11:338楼 得分:0
我想在这里问一下各位  
系统给类分配的这个“虚表”是放在哪里的呢?
他们不是属于类的吗??
发表于:2008-01-23 09:45:439楼 得分:0
第一段问题明白了,谢谢各位的支持。但是第二段问题还不清楚,cnzdgs   说:“类必须要有储存空间,所以对于空类会分配1字节空间。   ”
但是我不是很赞同,因为类还没实例化为对象的时候是不占用内存的。
所以麻烦大家再帮我分析第二段程序,帮我解决这个问题:  

为什么student对象stud的存储空间是1呢?这1字节内存存放的是什么东西呢?


谢谢
发表于:2008-01-23 09:57:2510楼 得分:0
debug下面仔细逛一逛就知道了
发表于:2008-01-23 10:10:1211楼 得分:0
mark.
发表于:2008-01-23 11:11:4812楼 得分:0
第二个问题的答案:
假设如楼主所想的那样,空类不占空间的话,那么请看下面的代码:
c/c++ code
class a{ public: show(){cout<<"a"<<endl; } class b{ public: show(){cout<<"b"<<endl; } class c{ public: show(){cout<<"c"<<endl; } class d{ public: show(){cout<<"d"<<endl; } int main(){ a a; b b; c c; d d; a.show(); b.show(); c.show(); d.show(); }

a,b,c,d分别是四个类,在main()函数中的四个实例a,b,c,d的地址肯定相同,但是后面四行分别调用它们的函数show()的时候,就无法区分到底是调用谁的show()函数,为了能够正确调用各自的函数,c++标准规定,类必须占用空间,所以为空类分配一个字节的空间.


快速检索

最新资讯
热门点击