C++在类的定义内部使用该类定义变量会怎样

C++语言 码拜 8年前 (2016-09-15) 1405次浏览
C++
在类的内部使用该类:
[code=c++]
class A
{
private:        A b;
int c;
};
A abc;
[/code]
像这样,变量adc在实例的时候内存计算(一层层迭代,无穷无尽)后岂不是无穷大了?有或不能这样用?
希望有人帮本人解答这个疑问
解决方案

15

这是不允许的。
9.2 Class Members
A class is considered a completely-defined object type (3.9) (or complete type) at the closing } of the
class-specifier.
 Within the class member-specification, the class is regarded as complete within function
bodies, default arguments, exception-specifications, and brace-or-equal-initializers for non-static data members (including such things in nested classes). Otherwise it is regarded as incomplete within its own class
member-specification.
10 Non-static (9.4) data members shall not have incomplete types. In particular, a class C shall not contain
a non-static member of class C, but it can contain a pointer or reference to an object of class C.

5

无限递归构造会死的,编译器也能检查出来,报错

10

类中不能申明该类的变量,否则你不能通过编译,但可以声明该类指针型变量

15

c++编译器已经考虑到这个无限递归的问题了,所以它根本不允许这种语法的出现,下图就是本人测试的结果:
C++在类的定义内部使用该类定义变量会怎样

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C++在类的定义内部使用该类定义变量会怎样
喜欢 (0)
[1034331897@qq.com]
分享 (0)