这个问题很简单。为了清楚起见,可以考虑下面的例子:类方法是否会增加类实例的大小?
// Note that none of the class have any data members
// Or if they do have data members, they're of equal size, type, and quantity
class Foo {
public:
void foo1();
void foo2();
// 96 other methods ...
void foo99();
};
class Bar {
public:
// Only one method
void bar();
};
class Derived1 : public Foo { };
class Derived2 : public Bar { };
int main() {
Foo f;
Bar b;
Derived1 d1;
Derived2 d2;
return 0;
}
执行情况f
,b
,d1
和d2
所有占据的空间相同数量的内存?作为对这个问题的延伸,理论上来说,在传递Foo
的时候复制实例需要的时间比Bar
长。
99种方法? arrgh .. – Nim
@Nim:我想真正明白这一点。;) – Zeenobit