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
-3
votes
3 answers

Why redeclaration of a static member is denied

I thought that we can redeclare a name any times in any context. But class A { static int a; static int a; }; returns a compile-time error: test.cpp:4:13: error: redeclaration of ‘int A::a’ test.cpp:3:13: note: previous declaration ‘int…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
-3
votes
1 answer

Use of Static in the example

I want to know the meaning of static in my file. Everytime, I come across an error and then find out that the word static is missed. Can you please explain when and where should the word static be used. Here, for example in the code, even if I…
user3345483
  • 19
  • 2
  • 7
-3
votes
1 answer

Why am I geting these bracket errors in my code?

I'm really not sure why it's doing this but it seems to be an issue with brackets. I'm getting the following errors while running this segment of code for Android in Eclipse: private static final String TWITTER_ACCESS_TOKEN_URL =…
stuart
  • 11
  • 4
-4
votes
2 answers

ERROR : undefined reference to classname::member_variable

Can't print value of static int variable I want to check the value of static variable in this context. I am using www.codechef.com/ide as IDE : C++14 (Gcc 6.3) as the Language + Compiler. CODE: #include using namespace std; class…
-4
votes
1 answer

C++ invalid use of non-static data member

I have looked at other questions regarding this error, but I so far have not found what looked like the answer to my problem there. I have two classes Message and ColorString. In a method of the former I make several instances of the latter by…
lo tolmencre
  • 3,804
  • 3
  • 30
  • 60
-4
votes
5 answers

Why have virtual static members not been added as a feature of C++?

I just read (for the k'th time) C++ static virtual members? Which is a question about simulating virtual static members. My question is - what made the C++ standards committe (or Bjarne Stroustrup before that) not add this feature to C? Are they…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
-4
votes
2 answers

Is there any difference between normal and static local variables in static methods?

class A { static void f(void) { int a; static int b; } }; Is there any (formal or practical) difference between a and b?
NPS
  • 6,003
  • 11
  • 53
  • 90
-4
votes
3 answers

Why does it seem as if my static constructor is only executed once?

When I have a static field in my class: public static int Counter = 0; With a static constructor: static Class() { Counter++; } When I create an object of this class and check Class.Counter it shows me 1 which is correct. But when I create…
Krzysztof Borowy
  • 538
  • 1
  • 5
  • 21
-5
votes
3 answers

Can I have a non-static data member in a static class?

I want to know if in a static class, all the methods and data member should be static or can I find a non static members?
Joy
  • 1,707
  • 7
  • 29
  • 44
1 2 3
78
79