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

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.

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;

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.

Why the use of const in a method with no parameters? [duplicate]

This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
What is meant with "const" at end of function declaration? [duplicate]
(6 answers)
Closed 8 years ago.
why is the purpose of "const" in that case?
std::string List::reqClubName() const
{
return m_Club;
}
Thanks
Banning the modification of members is not the only reason to qualify a member function as const. Whether you want to modify members or not, you can only call a member function on an object through a const context if the member function is marked const:
#include <iostream>
#include <string>
struct List
{
std::string reqClubName()
{
return m_Club;
}
private:
std::string m_Club;
};
int main()
{
const List l;
std::cout << l.reqClubName();
// ^ illegal: `l` is `const` but, `List::reqClubName` is not
}
Neither the language nor the compiler care that reqClubName doesn't try to modify the object anyway; your program will not compile.
Because of this, a const suffix should be your default approach unless you do need to modify a data member.
The const after a member function says that the function does not modify member data in the class it is a part of.

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.

Why does this const member function modifies the static member data? [duplicate]

This question already has answers here:
Why can a const member function modify a static data member?
(4 answers)
Closed 5 years ago.
I have the following line of code from Eckel-Thining in C++
Class Obj{
static int i,j;
public:
void f() const {cout<<i++<<endl;}
void f() const {cout<<i++<<endl;}
};
int Obj::i=47;
int Obj::j=11;
Now it's written in Ecekl for const member functions that by declaring a member function const , we tell the compiler to refrain from modifying a class data. I understand that in some specific cases like mutable const and by explicitly casting away constness of this pointer , we can do away with that but here neither of the two are happening and i++ and j++ working fine. Why is it so?
const is only for object (this pointer is const), modifying static members is allowed.
In a const member function, the object for which the function is called is accessed through a const access path; therefore, a const member function shall not modify the object and its non-static data members.
source:someone cites c++ standard
As you can see, static data member is not protected by const as per c++ standard.