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

How to access static member on instance?

Here is code, I've been struggling for hours with that, idea is to keep track of how many instances is created, but also make possible to call static method and change/update static member. There is similar question, but I can't implement any…
Alan Kis
  • 1,790
  • 4
  • 24
  • 47
19
votes
5 answers

how to see the values of static variables at runtime in visual studio

The question pretty much explains what I want to do. I have several projects in c# which constitute the solution and I want to view the values of static variables at runtime in visual studio. Is there a way to do that?
Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97
18
votes
2 answers

How do static member variables affect object size?

I'm wondering how static member variables are typically implemented in languages like C++ and if their use affects the size of instantiated objects. I know that a static members are shared by all instances of that class, but how is it shared? If it…
Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
18
votes
1 answer

Mixing constexpr declarations and const definitions

I came across the following situation: struct Foo { static constexpr char s[] = "Hello world"; }; const char Foo::s[]; This code snippet compiles with Clang 3.7 (with -std=c++11 and -std=c++14), but GCC (4.8, 6.0, same language settings) gives…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
18
votes
6 answers

C# two classes with static members referring to each other

I wonder why this code doesn't end up in endless recursion. I guess it's connected to the automatic initialization of static members to default values, but can someone tell me "step by step" how does 'a' get the value of 2 and 'b' of 1? public…
Jerry
  • 279
  • 3
  • 7
18
votes
2 answers

Proper initialization of static constexpr array in class template?

Static class members in C++ have caused a little confusion for me due to the standard's verbiage: 9.4.2 Static data members [class.static.data] The declaration of a static data member in its class definition is not a definition... However a…
monkey0506
  • 2,489
  • 1
  • 21
  • 27
17
votes
6 answers

Why field inside a local class cannot be static?

void foo (int x) { struct A { static const int d = 0; }; // error } Other than the reference from standard, is there any motivation behind this to disallow static field inside an inner class ? error: field `foo(int)::A::d' in local class cannot…
iammilind
  • 68,093
  • 33
  • 169
  • 336
17
votes
2 answers

Are Static classes thread safe

I have gone through msdn where it is written that all the static classes are thread safe. Well that article is meant for version 1.1... http://msdn.microsoft.com/en-us/library/d11h6832(v=vs.71).aspx All public static members (methods, properties,…
slash shogdhe
  • 3,943
  • 6
  • 27
  • 46
17
votes
1 answer

How should a Variable Template be referred to in C++14 when declared at Class scope?

For example: class example{ public: template static constexpr T var = T(1.5); }; int main(){ int a = example::var; example obj; int b = obj.var; return 0; } GCC produces error for both:…
José Luis
  • 397
  • 1
  • 11
17
votes
3 answers

Private static member in base class

#include #include class Base { static std::string s; }; template class Derived : Base { public: Derived() { std::cout << s << std::endl; } }; std::string Base::s = "some_text"; int…
Ashot
  • 10,807
  • 14
  • 66
  • 117
17
votes
2 answers

Constant expression initializer for static class member of type double

In C++11 and C++14, why do I need constexpr in the following snippet: class Foo { static constexpr double X = 0.75; }; whereas this one produces a compiler error: class Foo { static const double X = 0.75; }; and (more surprisingly) this…
Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
16
votes
4 answers

What is the scope of variables declared inside a static block in java?

Are variables declared inside a static block accessible anywhere else? What "kind" of member are they(ie., are they static member, too?)
One Two Three
  • 22,327
  • 24
  • 73
  • 114
16
votes
6 answers

static class data vs. anonymous namespaces in C++

I occasionally have classes with private static data members. I'm currently debating if I should replace these with static variables in an unnamed namespace in the implementation file. Other that not being able to use these variables in inline…
KeithB
  • 16,577
  • 3
  • 41
  • 45
16
votes
1 answer

Class-scoped enum

I have a c++ class with an enum inside, and I wanted to mimick that with boost::python, so that I can write MyClass.value in python. boost::python::class_ does not have an enum_ method, and I was looking for workarounds. I first tried with lambdas…
eudoxos
  • 18,545
  • 10
  • 61
  • 110
16
votes
13 answers

Why keyword 'this' cannot be used in a static method?

Why can't the keyword this be used in a static method? I am wondering why C# defines this constraint. What benefits can be gained by this constraint? [Update]: Actually, this is a question I got in an interview. I do know the usage of 'static' and…
airbai
  • 3,906
  • 5
  • 26
  • 30