About cpp's static member [duplicate] - c++

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.

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;

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.

Why pthread_mutex_t can't be static field of class ? [duplicate]

This question already has answers here:
Undefined reference to static class member
(9 answers)
C++ - Initialize and modify a static class member
(4 answers)
Closed 9 years ago.
I have problem with pthread_mutex_t. When I try to create static field pthread_mutex_t, then initialize it in static function and finally use it within some class methods I get many errors like:
main.o: In function `LogWriter::initialize(pthread_mutex_t*)':
main.cpp:(.text._ZN9LogWriter10initializeEP15pthread_mutex_t[LogWriter::initialize(pthread_mutex_t*)]+0x7): undefined reference to `LogWriter::mutex'
Simpplified class code:
class LogWriter{
static pthread_mutex_t mutex;
static void initialize(pthread_mutex_t *mut){
LogWriter::mutex = PTHREAD_MUTEX_INITIALIZER;
//if(pthread_mutex_init(&(LogWriter::mutex), NULL) != 0){
//init failed
//}
}
public:
static LogWriter getInstance(string module_name){
LogWriter instance(module_name);
return instance;
}
LogWriter& operator<<(string a);
};
My quesiton is: why ? I know that if I define it as normal (non-static) field I won't have any problems. Also searched google but I couldn't find any materials that are linked with this.
Also creating pointer to static pthread_mutex and initializing in in main function ends like this.
In some source file in your code, you need to add:
static LogWriter::pthread_mutex_t mutex;
The compiler won't "place" your variable in any particular source file, you have to do that for it. The class declaration just tells the compiler "I'll have a static variable somewhere" - but since, at least in theory, variables ordering and placement can make a difference [you may for example have different object files product "data" that goes into different sections of memory in some embedded system], the compiler won't be able to just throw it in any place it likes - that could be somewhere you don't want it.

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;
};