Why I can't define a static field in the declaration? [duplicate] - c++

This question already has answers here:
Static constant string (class member)
(11 answers)
How to initialize private static members in C++?
(18 answers)
Closed 2 years ago.
So it looks like I can't do this for some reason...
class MyClass
{
static std::string name = "Whatever";
};
I don't get it, for me it would make sense to define something in the declaration, even if is static, but instead, I have to do this... ¿?¿?¿?
class MyClass
{
static std::string name;
};
std::string MyClass::name = "Whatever";
And by the way... Isn't name private by default? Then why can I change its value from outside the class?

Why I can't define a static field in the declaration?
You can!
#include <string>
class MyClass
{
static inline std::string name = "Whatever";
// ^^^^^^
};
Isn't name private by default? Then why can I change its value from outside the class?
You can't!
You're defining/initialising it, not changing its value.

Related

Does a class string always gets initialized to empty? [duplicate]

This question already has answers here:
How do C++ class members get initialized if I don't do it explicitly?
(8 answers)
Value and size of an uninitialized std::string variable in c++
(5 answers)
Closed 2 years ago.
I have this string s and t in class.
class A
{
private:
static string s;
string t;
public:
void printString()
{
cout<<s<<t<<endl;
}
};
I tried printing these string and everytime they were empty if not initialized. So my question is whether a class string is always empty before initialization by a constructor. Also is it true for both static and non static strings?
In both cases, the default string constructor will be called. The default constructor for the std::string class will always initialize it to an empty string.

Static object of nested class [duplicate]

This question already has answers here:
What is the purpose of the Most Vexing Parse?
(6 answers)
Closed 2 years ago.
I have a Request_manager.h file declaring an object Tickets_Queue as static in Request_manager, which is a nested class from itself.
class Request_manager {
public:
class Tickets_Queue{
private:
pthread_mutex_t m_mutex;
public:
Tickets_Queue(){};
~Tickets_Queue(){};
};
static Tickets_Queue ticket_queue;
private:
static int m_connected;
};
To initialize it, in Request_manager.cpp I write:
int Request_manager::m_connected(0);
Request_manager::Tickets_Queue Request_manager::ticket_queue();
The initialization of m_connected works but for ticket_queue it says:
gcc.archive core/bin/gcc-5.4.0/debug/link-static/threading-multi/libcore.a
gcc.compile.c++ data_interfaces/bin/gcc-5.4.0/debug/link-static/threading-multi/Request_manager.o
data_interfaces/Request_manager.cpp:17:62: error: no ‘dataserver::Request_manager::Tickets_Queue dataserver::Request_manager::ticket_queue()’ member function declared in class ‘dataserver::Request_manager’
Request_manager::Tickets_Queue Request_manager::ticket_queue();
You are calling a function.
Yeah. My mistake. As #john says it is prototyping a function.
You have to declare the type and the variable:
Request_manager::Tickets_Queue Request_manager::ticket_queue;

Assign value to private static variable in a class [duplicate]

This question already has answers here:
Undefined reference to static class member
(9 answers)
How to initialize private static members in C++?
(18 answers)
Closed 6 years ago.
I have a file A.hpp as such:
class A
{
private:
static std::string s;
public:
void modify_string();
};
I am implementing this in a file A.cpp as such:
#include "A.hpp"
void A::modify_string()
{
s = "something"; // Error here.
}
My main class:
int main()
{
A a;
a.modify_string();
}
I understand static variables are shared by all the class instances. I also went through this SO post where it says how to access the static member. Public static member of class . Could you please let me know where my concept is missing at?
Edit:
I am getting this error:
error: undefined reference to A::s
When you define:
void modify_string() {
s = "something"; // Error here.
}
You are creating a new function, not defining the member function modify_string of the class A. You need to do:
void A::modify_string() {
To inform the compiler that you are defining the member function modify_string for class A.
You also need a ; after your class definition.
Finally, the variable s is static so it needs to be defined seperatly somewhere so the linker can find a reference to it. So add:
std::string A::s = "default";
This was clearly described in the link you provided for your question.
Here is a working example: http://ideone.com/iQ6Kux
You need to reserve storage for s in exactly one compilation unit.
Do that by writing
std::string A::s;
In exactly one source file.
Your definition void modify_string() {...} in A.cpp is not defining the member function of the class, it's defining a separate global function with the same name. You probably meant
void A::modify_string()
{
s = "something";
}

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.

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.