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
37
votes
4 answers

constexpr initializing static member using static function

Requirements I want a constexpr value (i.e. a compile-time constant) computed from a constexpr function. And I want both of these scoped to the namespace of a class, i.e. a static method and a static member of the class. First attempt I first wrote…
MvG
  • 57,380
  • 22
  • 148
  • 276
36
votes
9 answers

How to serialize static data members of a Java class?

When we serialize objects, static members are not serialized, but if we need to do so, is there any way out?
Maddy.Shik
  • 6,609
  • 18
  • 69
  • 98
35
votes
6 answers

When to use enums, and when to replace them with a class with static members?

It recently occured to me that the following (sample) enumeration... enum Color { Red, Green, Yellow, Blue } ... could be replaced with a seemingly more type-safe class: class Color { private Color() { } public static…
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
34
votes
1 answer

Can template classes have static members in C++

Can a template class in C++ have static members? Since it doesn't exist and is imcomplete before it is used, is this possible?
rubixibuc
  • 7,111
  • 18
  • 59
  • 98
34
votes
3 answers

Member variables in ES6 classes

Is there any way to use the ECMAScript6 class notation to declare either a static class variable or a default value for an instance variable? Without class what I have in mind would be written as function MyClass(arg) { if(arg) this.arg = arg;…
MvG
  • 57,380
  • 22
  • 148
  • 276
33
votes
4 answers

Why aren't static data members allowed in local classes?

What is the reasoning to why static const members cannot exist in local classes? It seems like a rather silly restriction. Example: void foo() { struct bar { int baz() { return 0; } // allowed static const int qux = 0; // not allowed?!? …
Pubby
  • 51,882
  • 13
  • 139
  • 180
33
votes
6 answers

Weird undefined symbols of static constants inside a struct/class

Either I'm very tired or something weird is happening that I'm not aware of, because the code below is resulting in undefined symbols for Foo::A and Foo::B when linking. This is minimized as much as I could from a larger project, but shows the…
murrekatt
  • 5,961
  • 5
  • 39
  • 63
32
votes
2 answers

g++ and clang++ different behaviour with recursive initialization of a static member

Given the following code: #include template struct foo { static std::size_t value; }; template <> std::size_t foo<0>::value = 0u; template std::size_t foo::value = 1u + foo::value; int main() { …
max66
  • 65,235
  • 10
  • 71
  • 111
32
votes
4 answers

Static fields vs Session variables

So far I've been using Session to pass some variables from one page to another. For instance user role. When a user logs in to the web application the role id of the user is kept in Session and that role is checked at different parts of the…
Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206
31
votes
1 answer

What is the correct way to initialize static data members in C++ (98, 11 and 14)

What is the right way to initialize static data members in C++? I'm also interested in how it has changed from C++98, to C++11 to C++14. Here is an example: // bufferedOutput.h class BufferedOutput { // Static member declaration. static long…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
31
votes
3 answers

Java: Overriding static variable of parent class?

I have the following class which I'm using as the base of all the models in my project: public abstract class BaseModel { static String table; static String idField = "id"; public static boolean exists(long id) throws Exception …
Ali
  • 261,656
  • 265
  • 575
  • 769
28
votes
5 answers

c++ access static members using null pointer

Recently tried the following program and it compiles, runs fine and produces expected output instead of any runtime error. #include class demo { public: static void fun() { std::cout<<"fun() is called\n"; …
Destructor
  • 14,123
  • 11
  • 61
  • 126
27
votes
2 answers

Why is a class allowed to have a static member of itself, but not a non-static member?

class base { public: base a; }; It gives compilation error. class base { public: static base a; }; whereas this code does not give compilation error
user966379
  • 2,823
  • 3
  • 24
  • 30
26
votes
8 answers

Why do members of a static class need to be declared as static? Why isn't it just implicit?

Obviously there can't be an instance member on a static class, since that class could never be instantiated. Why do we need to declare members as static?
richard
  • 12,263
  • 23
  • 95
  • 151
25
votes
2 answers

Is the ConcurrentDictionary thread-safe to the point that I can use it for a static cache?

Basically, if I want to do the following: public class SomeClass { private static ConcurrentDictionary<..., ...> Cache { get; set; } } Does this let me avoid using locks all over the place?
michael
  • 14,844
  • 28
  • 89
  • 177
1 2
3
78 79