What is the use of the Static Keyword? [duplicate] - c++

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.

Related

How is it that a static method in a class can be run without an object (as we know that a class does not have any memory allocated to it) in c++? [duplicate]

This question already has answers here:
C++: Difference Between Non-Member Function and Static Member Function?
(3 answers)
Closed 1 year ago.
So using static keyword below we can call MyMethod without creating an object of the class MyClass, my question is how is it even possible as a class is not allocated memory when the code is compiled?
static int MyMethod( int * a, int * b );
int one = 1;
int two = 2;
MyClass::MyMethod( &two, &one );
It seems you have a bit of a misunderstanding. There is no memory allocated for a class's methods every time it is made. Class methods are generally separate from their classes. I guess you could call the "Memory allocated to the member functions" the space that the opcodes take up. But yes, this is separate from the objects. It is just a language requirement that a member function that isn't static must be called by an instance of the class. Static functions intentionally don't care about this, and can be called even when it is not called by an object.

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;

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.

Correct syntax for array of static class method pointers [duplicate]

This question already has answers here:
How to initialize private static members in C++?
(18 answers)
Closed 7 years ago.
I'm new to function pointers and am getting hung up on syntax. What I'm trying to do is define, within a class, an array of functions to do string matching. The matching functions and their storing array will be static since they will be shared by all instances of the class. The functions are stored in an array so I can iterate through within match() and try different ones. Also, I'm trying to typedef the function pointer globally because similar matching functions will be used in many such classes. I've found some stuff suggesting that the signature should maybe be bool(Money::FP)(char str) but, if true, is there no way that I can define this globally (i.e. for classes other than "Money")?
The code below does not compile so please consider it as pseudocode for what I'm trying to accomplish.
Money.h:
typedef bool(*FP)(char* str);
class Money
{
private:
static FP matchers[3] = {
Money::m1,
Money::m2,
Money::m3
};
static bool m1(char* str);
static bool m2(char* str);
static bool m3(char* str);
public:
static void match(char* str);
};
It's not working because Money::m1 refers to Money type inside its declaration. Try to decouple them, eg
class Money {
private:
static FP matchers[3];
};
FP Money::matchers[3] = {
Money::m1,
Money::m2,
Money::m3
};
In any case you might consider using std::function<bool(char*)> instead that a function pointer, since you are working with C++. Performance is not an issue until you prove it to be an issue.

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.