Declaring vs Defining member variables C++ [duplicate] - c++

This question already has answers here:
What is the difference between a definition and a declaration?
(27 answers)
Closed 7 years ago.
I have seen many questions regarding definition and declaration of global and local variables and static members, but for non-static member variables, I am unable to distinguish it.
In the code below
class Line
{
int length; //?
static int L; //declared
}; //defined
We have defined the class Line, we have declared its static member L. So what about the non-static member length? Is it defined or declared?

For normal (automatic, non-static) variables, like length in your code, declaration without the extern keyword is also definition. So length is both declared and defined.

Related

What is the last line of this code doing? [duplicate]

This question already has answers here:
Initialize static variables in C++ class?
(9 answers)
Closed 2 years ago.
I have never seen this:
class myclass{
static int value;
};
int myclass::value(5);
This is a short version of a code that i see in the book C++ Concurrency in Action, but i don't get what is that declaration of a static class value out of the class block like that.
:: is known as scope resolution operator and one of the purpose of it is to access a class’s static variables outside class and it seems above piece of code is doing same initializing it outside class.
One of the ways to initialize variables in c++, known as constructor initialization, is done by enclosing the initial value between parentheses (()):
So int myclass::value(5); is equivalent to int myclass::value = 5;

thread_local "storage class specified" [duplicate]

This question already has answers here:
How can I have non-static thread-local variable for each instance
(3 answers)
Closed 3 years ago.
I declared a thread_local in the private section of my C++ class thread_local unsigned int counter_ = 0; and I keep getting an error "storage class specified for counter_"
From the C++ 17 Standard *12.2 Class members)
9 A member shall not be declared with the extern
storage-class-specifier. Within a class definition, a member shall not
be declared with the thread_local storage-class-specifier unless also
declared static.

Keyword const in class declarations [duplicate]

This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 7 years ago.
I understand that the keyword const marks variables, parameters etc as read only. In a class declaration why is const used at the end of a member declaration, for example:
QString getClassName() const;
Declaring a method const means that the method won't change the state of the object and thus it can be called on const instances of the class.
Notice that const-ness is enforced by the compiler. const methods can't modify member variables unless they are declared mutable. It's very common to have mutable locks so that const methods can still be synchronized.
It basically means that the function is "promising" that it won't change the calling object.

About cpp's static member [duplicate]

This question already has answers here:
Undefined Reference to class static member in static member
(1 answer)
How to initialize private static members in C++?
(18 answers)
Closed 8 years ago.
Class base{
public :
static vector<int> _elems;
...
How can I use that static one.Must I define it out of the class body again?
Or I meet a trouble about a error"Undefine reference to 'base::_elems'"
You've only declared the static member, never defined it. In your cpp file you need to do this:
vector<int> base::_elems;
You can use it like any other variable. You only need to remember that the static variable is the same for all instances.
Edit: I forgot the defenition. You must define the variable, this can be done from any cpp file, but i recommend to define the variable in the file base.cpp.

Why does c++ class need to define static field(data member) outside the class scope? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Initializing private static members
Why I can't initialize non-const static member or static array in class?
It is strange to me. Why not assume there is a static field at the global scope?
It has to be placed somewhere (in some object file), so linker could find it. If you have declaration of class with static filed in .h file and include this file in a few .cpp files, then it would be ambiguous, which object file should have place allocated for this filed.
Please also note, that primitive type const static field could be initialized in class declaration:
class Foo
{
static const int n = 42;
};