考虑:
class A
{
public:
virtual void update() = 0;
}
class B : public A
{
public:
void update() { /* stuff goes in here... */ }
private:
double a, b, c;
}
class C {
// Same kind of thing as B, but with different update function/data members
}
Run Code Online (Sandbox Code Playgroud)
我现在在做:
A * array = new A[1000];
array[0] = new B();
array[1] = new C();
//etc., etc.
Run Code Online (Sandbox Code Playgroud)
如果我调用sizeof(B),返回的大小是3个双成员所需的大小,加上虚函数指针表所需的一些开销.现在,回到我的代码,结果是'sizeof(myclass)'是32; 也就是说,我的数据成员使用24个字节,虚拟功能表使用8个字节(4个虚函数).我的问题是:有什么办法可以简化这个吗?我的程序最终会使用大量的内存,我不喜欢它的25%被虚拟函数指针吃掉的声音.