使用子类对象初始化的多态基类对象数组
抱歉标题太复杂了。我有这样的东西:
class Base
{
public:
int SomeMember;
Base() : SomeMember(42) {}
virtual int Get() { return SomeMember; }
};
class ChildA : public Base
{
public:
virtual int Get() { return SomeMember*2; }
};
class ChildB : public Base
{
public:
virtual int Get() { return SomeMember/2; }
};
class ChildC : public Base
{
public:
virtual int Get() { return SomeMember+2; }
};
Base ar[] = { ChildA(), ChildB(), ChildC() };
for (int i=0; i<sizeof(ar)/sizeof(Base); i++)
{
Base* ptr = &ar[i];
printf("El %i: %i\n", i, ptr->Get());
}
哪个输出:
El 0: 42
El 1: 42
El 2: 42
这是正确的行为(在 VC++ 2005 中)吗?老实说,我预计这段代码不会编译,但它确实编译了,但它没有给我我需要的结果。这有可能吗?
Sorry for the complicated title. I have something like this:
class Base
{
public:
int SomeMember;
Base() : SomeMember(42) {}
virtual int Get() { return SomeMember; }
};
class ChildA : public Base
{
public:
virtual int Get() { return SomeMember*2; }
};
class ChildB : public Base
{
public:
virtual int Get() { return SomeMember/2; }
};
class ChildC : public Base
{
public:
virtual int Get() { return SomeMember+2; }
};
Base ar[] = { ChildA(), ChildB(), ChildC() };
for (int i=0; i<sizeof(ar)/sizeof(Base); i++)
{
Base* ptr = &ar[i];
printf("El %i: %i\n", i, ptr->Get());
}
Which outputs:
El 0: 42
El 1: 42
El 2: 42
Is this correct behavior (in VC++ 2005)? To be perfectly honest, I expected this code not to compile, but it did, however it does not give me the results I need. Is this at all possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是正确的行为。原因是
通过将三个不同类的对象复制到
class Base
的对象上来初始化数组元素,并生成class Base
的对象,因此您可以观察class Base 的行为
来自数组的每个元素。如果你想存储不同类的对象,你必须使用 new 来分配它们并存储指向它们的指针。
Yes, this is correct behavior. The reason is
initializes array elements by copying objects of three different classes onto objects of
class Base
and that yields objects ofclass Base
and therefore you observe behavior ofclass Base
from each element of the array.If you want to store objects of different classes you have to allocate them with
new
and store pointers to them.要实现您期望的多态行为,您应该使用指向
Base
的指针数组并通过new
创建对象:To achieve polymorphic behaviour you expected, you should use array of pointers to
Base
and create objects vianew
:实际发生的情况是:
由于 ar[] 的类型是 Base,因此为 ar 分配了 3*sizeof(Base) 内存量。
由于您没有为 Base 声明显式复制构造函数,因此将调用 base 的默认复制构造函数,该构造函数只是将 ChildA、ChildB 和 ChildC 对象的“Base”部分按位复制到数组 ar 中包含的 Base 对象中(默认复制构造函数足够聪明,不会将 Child 对象的虚拟指针按位复制到 Base 虚拟指针中。
ar[0]、ar[1] 和 ar[2] 的虚拟指针指向 Base::Get,因此调用 Base::Get。
这里需要注意的是,对象的虚拟指针所指向的函数在执行之前总是已知的。
在这种情况下,运行时预先知道 arr 由“Base”对象组成,因此它会设置它们的 vptr,以便在分配内存后立即指向 Base::Get。
What is actually happening is that:
Since the type of ar[] is Base, 3*sizeof(Base) amount of memory is allocated to ar.
Since you have not declared an explicit copy constructor for Base, the default copy constructor of base is called which just bitwise copies the "Base" part of ChildA, ChildB and ChildC objects into the Base objects contained in the array ar( The default copy constructor is smart enough not to bitwise copy the virtual pointer of Child objects into the Base virtual pointer ).
ar[0], ar[1] and ar[2]'s virtual pointers point to Base::Get so Base::Get is called.
The point to note here is that the function to which virtual pointer of an object will point is always known prior to the execution.
In this case, the runtime knew in advance that arr consists of "Base" objects, so it it set their vptr so as to point to Base::Get as soon as they were alotted memory.