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

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;

Related

Object creation with parameters not possible in c++? [duplicate]

This question already has answers here:
Why can't member initializers use parentheses?
(2 answers)
Initialization of member variable via parentheses doesn't work [duplicate]
(1 answer)
can not define and init a class member by Parentheses [duplicate]
(1 answer)
Closed 9 months ago.
I'm trying to neatly create a objects within a class and I've run into what seems to me to be an odd limitation in c++, and I wondered if I'm just missing a syntax trick or whether it's truly impossible; specifically it seems like I cannot explicitly instantiate a class object inside another class if the constructor of the former has parameters (the parameters are constant) generating the odd message: 'Expected parameter declarator'
It seems as though only default constructors are supported in this scenario, or am I missing a bit of magic?
Currently using c++17 (simply because that's the default in this IDE)
class Fred
{
public:
Fred(const int i)
{
}
Fred()
{
}
};
Fred fred1(0); // This compiles
class Charlie
{
Fred fred2(); // This compiles
Fred fred3(0); // This does not compile
};

What is the use of the Static Keyword? [duplicate]

This question already has answers here:
Static variables in member functions
(4 answers)
The static keyword and its various uses in C++
(9 answers)
Closed 3 years ago.
I am studying for my finals and there is a topic which is bugging me recently i don't understand why we use static variables or static data members in our code. Can anyone explain it to me how and why we use the static keyword in C++
I have tried looking it up on different sites and i have tried some code, what i don't understand is why am i getting such results.
class myclass {
public:
int a,b;
inline int getVal();
};
inline int myclass :: getVal()
{
cout<<"Enter the value of a and b\n";
static int a = 9; //static keyword used.
cin>>a>>b;
}
int main()
{
myclass o1;
o1.getVal();
cout<<"\nThe value of a is : "<<o1.a<<"\nThe value of b is : "<<o1.b;
}
the value i am getting for a is 3, no matter what i enter? can anyone explain to me why is that?
The meaning of static is this: When a variable is declared as static, space for it gets allocated for the lifetime of the program. Even if the function is called multiple times, space for the static variable is allocated only once and the value of variable in the previous call gets carried through the next function call.
Starting point
The static keyword means that a variable is bound to a class itself instead of an object of the class.
If it is not declared as static it can be changed for each object of the class individually whereas if you change a static variable of a class it is set to the new value for all of its objects.
I hope this helps.
If you have any questions feel free to ask.
Edit: as Bogdan Doicin pointed out in another answer static has another meaning in functions.
I will leave this up for the other meaning but if you want to accept an answer accept his because it better fits the question.

static member vs static const member instantiation - what's the difference? [duplicate]

This question already has answers here:
Why can't I initialize non-const static member or static array in class?
(5 answers)
Closed 7 years ago.
What exactly is the difference between these two members in question of initialization and instantiation?:
class Test {
// ctor, dtor, ... here
private:
static int m_test = 0; // error - non-const ...
static const int m_const_test = 0; // working - const ...
};
Why is initialization of non-const members not valid?
And when will both variables (assuming non-const member without init!) be instantiated?
Edit: Even if a good part of this question is superimposable to the mentioned posts, i don't think that every part of my question is answered in these entries.
BR, Tuna
If multiple compilation units include your class definition then in which one should be your static int m_test = 0 be placed? For const static ... it does not matter because it is const.
m_const_test being declared as const can´t be changed.
So, you can save a little bit of time using m_const_test, but is not as flexible as m_test.

Declaring vs Defining member variables C++ [duplicate]

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.

setting static member variable inside a static method [duplicate]

This question already has answers here:
Undefined reference to static variable [duplicate]
(2 answers)
Closed 9 years ago.
I am beginner to C++ and have a doubt about static member variables and member functions.
I have implemented a class as follows -
class Foo
{
private:
static int myVariable;
public:
static void setMyVariable()
{
myVariable = 100;
}
static void resetMyVariable()
{
myVariable = 0;
}
};
There are following considerations when I wrote a code like that -
I want only one instance of class Foo. Thats why I made all member variables and functions as static.
I don't want the outside code to touch myVariable
I have put this class in a header file and included in my main file. When I do this, I get an error undefined reference to Foo::myVariable
I want to know if I can write a code which can satisfy above requirements?
Thanks !
You need to define static class variables somewhere:
e.g. in your main C++ file,
int Foo::myVariable;
Note that technically, by making everything static, you may have no instances of Foo.