我clone()
对以下示例中的方法有疑问。我预计这sizeof(*((*ptr1).clone()))
将与大小相同,b1
但它们不是。他们是。我错过了什么?sizeof(*(ptr2.clone()))
c1
sizeof(a1)
这是代码。
class A
{int a;
public:
virtual A * clone() {return this;}
};
class B : public A
{int b,c;
public:
B * clone() {return this;}
};
class C : public A
{int b,c,d;
public:
C * clone() {return this;}
};
int main()
{
A a1;
B b1;
C c1;
A * ptr1 = &b1;
A & ptr2 = c1;
std::cout << "sizeof(a1) = " << sizeof(a1) << '\n';
std::cout << "sizeof(b1) = " << sizeof(b1) << '\n';
std::cout << "sizeof(*(b1.clone())) = " << sizeof(*(b1.clone())) << '\n';
std::cout << "sizeof(c1) = " << sizeof(c1) << '\n';
std::cout << "sizeof(*((*ptr1).clone()))" << sizeof(*((*ptr1).clone())) << '\n';
std::cout << "sizeof(*(ptr2.clone()))" << sizeof(*(ptr2.clone())) << '\n';
return 0;
}