C++ Why class static member are considered declaration while instance member are not? [duplicate]

  c++, c++11

Consider the following code as C++ Primer 5th Ed. section 7.6.

class Bar {
public:
    // ...
private:
    static Bar mem1; // ok: static member can have incomplete type --> Tag 1
    Bar *mem2;       // ok: pointer member can have incomplete type --> Tag 2
    Bar mem3;        // error: data members must have complete type --> Tag 3
    int x=4; --> Tag 4
}
  1. The above class definition considered to be a definition. This so call definition has NO STORAGE allocated like a created object.
  2. Tag 1 & 2 works because Bar are considered declaration. I am good with that since there’s no object of class Bar created. These are declaration within the class definition.
  3. Tag 3 is where i have trouble with. To me tag 3 is considered declaration because no instance of class Bar is created yet.
  4. Tag 4 – i will also considered this a declaration at this stage since no instance of class Bar is created.

Point is those data members like tag 3 & 4, i would considered them as definition only if instance of class Bar is created outside of the class definition.

Bar bar; --> this would then initialize tag 3 & 4 --> Tag 5

Why is tag 3 needs to be complete as at this line there is no storage allocated since it’s still a declaration? Tag 3 is not an instance yet at this line. Tag 3 only becomes a definition and have storage alloacated when we create the class Bar outside of the class definition as in Tag 5.

Source: Windows Questions C++

LEAVE A COMMENT