静态类成员的类自己的类型 [英] static class member of class's own type

查看:158
本文介绍了静态类成员的类自己的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:



类中的静态成员的内存分配


一个类不会被定义,直到它的类体完成,具有自己类型的数据成员。类可以具有作为其自身类型的指针/引用的数据成员。




  • C ++ Primer(Lippman Lajoie)



  



class justAClass
{
public:
justAClass();

private:
static justAClass justAMember;
}

对于指针是可以理解的。但是这个上面的事情将如何工作呢?我将如何决定这种类的对象的大小?是否是递归的情况下(没有基本条件)拥有自己的类型的成员,即使它是静态的?

解决方案

类的原因不能有自己类型的数据成员是编译器必须知道类对象的大小。
例如,一个类是函数中的一个局部变量,编译器只能处理栈的大小。



对于你的情况,类成员不驻留在类对象中,因此对类对象的大小没有影响。没关系。


Possible Duplicate:
Do static members of a class occupy memory if no object of that class is created?
Memory Allocation of Static Members in a Class

"A class is not considered defined untill its class body is complete, a class can not have data members of its own type. A class can have data members that are pointers/reference to its own type."

  • C++ Primer (Lippman Lajoie)

Makes sense.

But why is this allowed then ?

class justAClass
{
     public  : 
     justAClass();

     private :          
     static justAClass justAMember;
}

For pointers it is understandable. But how will this above thing work ? How will i ever decide the size for object of such a class ? Isnt it a recursive case (with no base condition) to have a member of its own type, even if it is static ?

解决方案

The reason for class can't have data members of its own type is the compiler must know the size of class object. For example, one class is a local variable in function, the compiler can handle the stack only it knows the class size.

For your case, the static class member doesn't reside in class object, so has no impact to size of class object. It's OK.

这篇关于静态类成员的类自己的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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