使用子类对象初始化的多态基类对象数组

发布于 2024-12-01 16:33:19 字数 735 浏览 3 评论 0原文

抱歉标题太复杂了。我有这样的东西:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇柔作态 2024-12-08 16:33:19

是的,这是正确的行为。原因是

Base ar[] = { ChildA(), ChildB(), ChildC() };

通过将三个不同类的对象复制到 class Base 的对象上来初始化数组元素,并生成 class Base 的对象,因此您可以观察 class Base 的行为 来自数组的每个元素。

如果你想存储不同类的对象,你必须使用 new 来分配它们并存储指向它们的指针。

Yes, this is correct behavior. The reason is

Base ar[] = { ChildA(), ChildB(), ChildC() };

initializes array elements by copying objects of three different classes onto objects of class Base and that yields objects of class Base and therefore you observe behavior of class 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.

九歌凝 2024-12-08 16:33:19

要实现您期望的多态行为,您应该使用指向 Base 的指针数组并通过 new 创建对象:

Base* ar[] = { new ChildA(), new ChildB(), new ChildC() };

To achieve polymorphic behaviour you expected, you should use array of pointers to Base and create objects via new:

Base* ar[] = { new ChildA(), new ChildB(), new ChildC() };
赠我空喜 2024-12-08 16:33:19

实际发生的情况是:

  1. 由于 ar[] 的类型是 Base,因此为 ar 分配了 3*sizeof(Base) 内存量。

  2. 由于您没有为 Base 声明显式复制构造函数,因此将调用 base 的默认复制构造函数,该构造函数只是将 ChildA、ChildB 和 ChildC 对象的“Base”部分按位复制到数组 ar 中包含的 Base 对象中(默认复制构造函数足够聪明,不会将 Child 对象的虚拟指针按位复制到 Base 虚拟指针中。

  3. ar[0]、ar[1] 和 ar[2] 的虚拟指针指向 Base::Get,因此调用 Base::Get。

这里需要注意的是,对象的虚拟指针所指向的函数在执行之前总是已知的。

在这种情况下,运行时预先知道 arr 由“Base”对象组成,因此它会设置它们的 vptr,以便在分配内存后立即指向 Base::Get。

What is actually happening is that:

  1. Since the type of ar[] is Base, 3*sizeof(Base) amount of memory is allocated to ar.

  2. 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 ).

  3. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文