Why is a class allowed to have a static member of itself, but not a non-static member?

后端 未结 2 1343
栀梦
栀梦 2020-12-01 18:16
class base {
public:
    base a;
};

It gives compilation error.

class base {
public:
    static base a;
};

wherea

2条回答
  •  感情败类
    2020-12-01 18:43

    Because static class members are not stored in the class instance, that's why a static would work.

    Storing an object inside another object of the same type would break the runtime - infinite size, right?

    What would sizeof return? The size of the object needs to be known by the compiler, but since it contains an object of the same type, it doesn't make sense.

提交回复
热议问题