Questions tagged [static-members]

A static member is a data field that is shared by all instances of a class or structure for the same program. Static member exists even when no objects of the static data member's class exist. A feature of C++, roughly equivalent to Java static fields.

Static member can be accessed using type qualifier, even if there is no instance of the class. They must be declared outside the class declaration:

// class declaration:
class a_class  { static int sf;  };

// field declaration:
int a_class::sf = 17;

Apart data fields, it can be static methods that have no access to the non-static fields but for that can be invoked without having the instance, also by type qualifier:

struct a_struct { static long the_function(); };
...
long v = a_struct::the_function();
1180 questions
25
votes
6 answers

How do you create a static template member function that performs actions on a template class?

I'm trying to create a generic function that removes duplicates from an std::vector. Since I don't want to create a function for each vector type, I want to make this a template function that can accept vectors of any type. Here is what I…
bsruth
  • 5,372
  • 6
  • 35
  • 44
25
votes
3 answers

C++ definition of dllimport static data member

I do have a class which looks like below: //.h file class __declspec(dllimport) MyClass { public: //stuff private: static int myInt; }; // .cpp file int MyClass::myInt = 0; I get the following compile error: error C2491:…
MBZ
  • 26,084
  • 47
  • 114
  • 191
25
votes
4 answers

F# code organization: types & modules

How do you decide between writing a function inside a module or as a static member of some type? For example, in the source code of F#, there are lots of types that are defined along with a equally named module, as follows: type MyType = //…
Bruno Reis
  • 37,201
  • 11
  • 119
  • 156
24
votes
6 answers

How to force a static member to be initialized?

Consider this example code: template char register_(){ return D::get_dummy(); // static function } template struct Foo{ static char const dummy; }; template char const Foo::dummy = register_(); struct Bar …
Xeo
  • 129,499
  • 52
  • 291
  • 397
24
votes
3 answers

Why must non-integral static data members initialized in the class be constexpr?

Static integral data members initialized in the class definition may be declared const or constexpr, but non-integral static data members initialized in the class definition must be constexpr: class MyClass { static const int w = 5; …
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
24
votes
3 answers

Java Homework Help (Accessing Static Member via Instance Reference)

Here is my homework question: Write a class declaration for a class “Clock”. It should have instance variables for hours, minutes, seconds (all integers). It should also have a toString() method to show the time in the format shown below. Write a…
Bit Deception
  • 297
  • 1
  • 3
  • 7
23
votes
2 answers

Is it possible to declare a virtual static constant value in a C++ class?

I'd like to have a base class that has a constant field (like an unique ID associated with the class that can't be modified after compile time). So far the static const declaration would be just fine. Now, I'd like to inherit this base class and…
Siska Ádám
  • 417
  • 1
  • 5
  • 11
22
votes
5 answers

static vs extern "C"/"C++"

What is the difference between a static member function and an extern "C" linkage function ? For instance, when using "makecontext" in C++, I need to pass a pointer to function. Google recommends using extern "C" linkage for it, because…
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
21
votes
3 answers

Why should I initialize static class variables in C++?

In C and C++ all static variables are initialized by default to ZERO. This is not the case of static class data members. Why is that? #include using namespace std; int var; class MyClass { public: static int classVar; }; int…
Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219
21
votes
3 answers

If multiple classes have a static variable in common, are they shared (within the same scope?)

I have the following example code: class A { public: static int a; }; int A::a = 0; class B { public: static A a1; }; A B::a1; class C { public: static A a1; }; A C::a1; int main(int argc, const char * argv[])…
syko
  • 3,477
  • 5
  • 28
  • 51
21
votes
6 answers

static const in c++ class: undefined reference

I have a class for local use only (i.e., its cope is only the c++ file it is defined in) class A { public: static const int MY_CONST = 5; }; void fun( int b ) { int j = A::MY_CONST; // no problem int k = std::min( A::MY_CONST, b…
user2379182
21
votes
4 answers

error LNK2001: unresolved external symbol "private: static class

error LNK2001: unresolved external symbol "private: static class irrklang::ISoundEngine * GameEngine::Sound::_soundDevice" (?_soundDevice@Sound@GameEngine@@0PAVISoundEngine@irrklang@@A) I cannot figure out why i am receiving this error. I believe…
Robbie
  • 294
  • 1
  • 2
  • 14
21
votes
5 answers

Why static fields (not final) is restricted in inner class in java

Possible Duplicate: Why does Java prohibit static fields in inner classes? I was going through the specification and got that it is not possible to have the static member in the inner class which is not final compile time constant . class…
Virendra
  • 387
  • 1
  • 7
  • 15
19
votes
3 answers

static member variable when declared private

When a static member variable is declared private in a class, how can it be defined? Suppose i have the following class declaration class static_demo { private: static int a; public: static int b; void set(int x, int y) …
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
19
votes
1 answer

"Invalid use of non-static data member" when initializing static member from global variable

class A { int x; static int i; }; int x = 10; int A::i = x; When I compile the code above, it get the error :8:12: error: invalid use of non-static data member 'A::x' 8 | int A::i = x; | ^ :2:9: note:…
YuCL Lan
  • 193
  • 1
  • 5