为什么结构不能包含其自身的实例? [英] Why structure can not contain Instance of itself?

查看:48
本文介绍了为什么结构不能包含其自身的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到有关c ++中的结构的信息,该结构不能包含其自身的实例.有人可以帮助我了解为什么它不能包含其自身的实例吗?

I read about structure in c++ that it can not contain instance of itself. Can anybody help me to understand why it can not contain instance of itself?

推荐答案

由于要创建它的实例,因此需要创建变量,它本身就是它的实例-它将调用构造函数.

Because to create the instance of it, you will need to create the variable, which is itself an instance of it - which will invoke the constructor.

这将导致对构造函数的无限递归调用.

This will result in infinite recursive call to the constructor.

假设 class A 有一个名为 a 的实例变量:
调用 A 的构造函数将导致 a 的初始化,而a 本身就是 A .为此,将再次调用 A 的构造函数.

Assume class A has an instance variable named a:
Invoking the constructor of A will cause the initialization of a, which is itself an A. To do it - the constructor of A will be invoked again.

请注意,它甚至不会编译,因为编译器无法为其分配内存,也不知道为每个对象分配多少空间.存储实例变量 a 需要多少空间?[任何有限的空间都是不够的,因为总会有一个额外的变量需要分配]

Note that it will not even compile because the compiler cannot allocate the memory for it, it doesn't know how much space to allocate for each object. How much space does it take to store the instance variable a? [Any finite space will not be enough because there will always be an extra variable which also needs to be allocated]

这篇关于为什么结构不能包含其自身的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆