1

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
Jason
  • 36,170
  • 5
  • 26
  • 60
  • [Related](https://stackoverflow.com/questions/61856487/confusion-about-declaration-and-definition-of-static-const-data-memebers), but about `static` memebers rather than non-`static`. – Enlico Jan 09 '22 at 13:07

1 Answers1

2

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

The declaration of a non-static data member (such as x) is a definition.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Question: would this be considered a declaration or definition? `MyClass* p = nullptr` – Irelia Jan 09 '22 at 13:06
  • @Irelia I think `MyClass* p = nullptr` is a definition. – Jason Jan 09 '22 at 13:16
  • @Irelia Both. It is a definition, and a definition is a type of declaration. – Peter Jan 09 '22 at 13:16
  • @eerorika So why doesn't [this](https://godbolt.org/z/jM8697sWs) intiantiate the class template `Name<>`for `char`? For example, consider this code: `template struct Name { T x{}; }; struct Person { Name c; //if this is a definition then why doesn't this intiantiate the class template Name<> for char? };` So should i ask a separate question on SO for that? – Jason Jan 17 '22 at 12:47
  • @Mike It does instantiate the class template. – eerorika Jan 17 '22 at 13:07
  • @eerorika Did you check the [link](https://godbolt.org/z/jM8697sWs) i mentioned in my above comment. There is no code is generated for the non-static data member `c`. On the other hand if i add `Name p;` inside main() function then it does intiantiate `Name<>` for char. Can you give me some link where it does generate for non-static member also. Or explain if i am wrong here in the comments. – Jason Jan 17 '22 at 13:26
  • @Mike Why would there be code generated for a non-static data member? You can use for example a static_assert to verify that a class template is being instantiated: https://godbolt.org/z/Gv6s4WKox – eerorika Jan 17 '22 at 13:29
  • @eerorika I mean when we define a variable then storage is allocated for it. So that is why i thought that if the declaration for a non-static data member is also a definition then it should allocate space for that non static data member. But i know that when we create object of that class type then only the storage will be allocated. So in this sense, this definition of nonstatic data member is slightly different from usual meaning of defining an object right? – Jason Jan 17 '22 at 14:49