This question already has answers here:
How to initialize private static members in C++?
(18 answers)
Closed 8 years ago.
I have a problem where I have declared a static string vector in the .h file inside a class ,
.h file
static std::vector<std::string> VHDSigBuffer;
How to use this vector in my class function Implementations in cpp file?
In my_class.h header
class my_class
{
public:
// Declaration
static std::vector<std::string> VHDSigBuffer;
};
In my_class.cpp implementation
// Definition
std::vector<std::string> my_class::VHDSigBuffer;
Now you can freely use my_class::VHDSigBuffer.
You need to instantiate it within your CPP file, with a line like this:
std::vector<std::string> MyClass::VHDSigBuffer;
This line will be outside any function definition in the CPP file.
If I understand what you're saying correctly,
If you want to use this vector in its own class you can access it as VHDSigBuffer, or if you want to use it in another class you can access it as youClass::VHDSigBuffer.
Related
This question already has answers here:
How to have static data members in a header-only library?
(3 answers)
Closed 6 years ago.
I am developing lightweight parser as C++ h-file template library.
Gramma is described in specific BNF-like notation using overloaded operators on some classes which should be enumerated somehow. I need just one global variable as some counter performing it.
I do not want to use extern int var; in h-file and int var; in cpp-file because all my stuff in single header file and now the user just needs to include it.
I can declare static int var; in header file but copy of this variable appears in all object files where my header file is included.
Is it OK for template library?
Any suggestions?
As already mentioned you can use singleton pattern.
This version doesn't require definition of static member in template cpp file.
template <typename T> class Tmpl
{
public:
static Tmpl<T>& GlobalInstance()
{
static Tmpl<T> m_Singleton;
return m_Singleton;
};
};
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.
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.
This question already has answers here:
Declaring static data members of normal class and class template
(2 answers)
Closed 9 years ago.
// A.h
class A {
public:
static int a;
};
int A::a = 0;
If I try to include A.h in multiple .cpp files, link would fail with multiple definition of A::a. I think this makes sense because each .obj file contains A::a
However, I could use a template,
// A.h
template<class T>
class A {
public:
static T a;
};
template<class T>
T A<T>::a = 0;
I can now include A.h in multiple .cpp files and also I can assign the value A<int>::a = 100; in one .cpp file and get the same value in another one with A<int>::a.
When does template make such difference?
Is there only 1 copy of this static variable? Which .obj will keep this variable?
Is the constructor called only once? If the initial value is different, which one wins?
When does template make such difference?
Always. I suppose I don't understand the question.
Is there only 1 copy of this static variable?
Only one copy in the final program for each distinct type T the template was instantiated with.
Which .obj will keep this variable?
All of them that were generated from translation units where the template was instantiated. The linker then chooses one and discards all others.
Is the constructor called only once?
Once for each specialization.
If the initial value is different, which one wins?
That would be a violation of the One Definition Rule. Such a program would be ill-formed, no diagnostic required.
Why not define static member in the souce file that implements the class A? Then you should be able to include A.h in multiple source files w/o problem.
// A.h
class A {
public:
static int a;
};
// A.cpp
int A::a = 0;
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;
};