Search code examples
c++c++11declarationdefinitionnon-static

Is a declaration for a non-static data member of a class type also a definition?


I am learning C++. In particular, the difference between declaration and definition. To my current understanding

All definitions are declarations but not all declarations are definitions.

Here is an example of what I currently know.

int x; //definition(as well as declaration according to the above quote)
extern int y; //declaration that is not a definition 

class CustomType
{
    int x; //is this a "declaration that is not a definition" OR a "definition"
};
int main()
{
   int p[10]; //definition
   //...
}

I want to know whether int x; inside the class' definition is a declaration that is not a definition or a definition. And where in the standard is this stated so that I can read more about it.


Solution

  • I want to know whether int x; inside the class' definition is a declaration that is not a definition or a definition.

    The declaration int x; of the data member in your example is also a definition. The standard even has an example showing exactly this.

    From basic.def#example-1

    Example 1: All but one of the following are definitions:

    struct X {                      // defines X
     int x;                        // defines non-static data member x
    };
    

    As you can see from the above quoted reference, the declaration int x; of the non-static member is also a definition. This is because none of the items in basic.def#2 applies to data member int x;.