いいえ。a
とb
はまったく同じサイズです (b
へのポインタは格納されませんfunc
)。
C++ のクラス関数は、オブジェクト自体からリンク (ポイント) されるのではなく、単に他の関数として格納されます。クラス関数を呼び出すときは、通常の関数を呼び出しているだけです (ポインターから呼び出しているわけではありません)。これがb.func = another_func;
、C++ で次のようなことを行うことが違法である理由です。
これをコードで説明するには:
/////////
// C++
/////////
struct B {
int a;
int b;
int func() {
return this->a + this->b;
}
};
B b;
b.func();
//////////////////////////////
// Example compile output if it was compiled to C (not actual compile output!)
// i.e. this C code will do the equivalent of the C++ code above
//////////////////////////////
struct B {
int a;
int b;
};
int B_func(struct B* this) {
return this->a + this->b;
}
B b;
B_func(&b);
// This should illustrate why it is impossible to do this in C++:
// b.func = another_func;