這個問題很簡單。爲了清楚起見,可以考慮下面的例子:類方法是否會增加類實例的大小?
// 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