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
-2
votes
1 answer

Is it possible to set the class functions in the two functions?

I don't know how to call my class functions into printData(Testscore&) and readData(TestScore). Also, could someone tell me why my Average() isn't being called to the main? I just learned about using static member variables and static member…
RocketKatz
  • 39
  • 1
  • 9
-2
votes
2 answers

Maintain different states for a static field in multiple objects

I have a class Point, that has a static type variable Visual. I made it static because: The type Visual contains method to draw points and lines between points, etc. The type Visual also creates a blank canvas each time a Visual object is…
Muye
  • 175
  • 1
  • 7
-2
votes
1 answer

Avoiding static variables and use a clear design way to code

Consider the following three classes - Class A { static DrMag drmag; //Avoid static variables public DrMag validateCheck() { drmag.setName("Kane"); drmag.setType("Student"); drmag.setMarks(100); …
sjain
  • 23,126
  • 28
  • 107
  • 185
-2
votes
2 answers

How to access a static variable from Parent form to child form in c#

How can I access static variable from Parent form to child form?
DPM
  • 67
  • 2
  • 14
-2
votes
1 answer

How to get Class definition in static member variable initialization?

I have this code working, but want to improve it: public class VisibleComponent extends Component { static private var itz:Initializer = new Initializer(VisibleComponent); // ... omitted } public class Initializer { public function…
Marson Mao
  • 2,935
  • 6
  • 30
  • 45
-2
votes
2 answers

Undefined reference to defined static private variable

i've got a problem dealing with static private variables. Here is my code. ClassA.h: class ClassA{ static int a; public: int getA(); }; Class.cpp: #include "ClassA.h" int ClassA::a = 9001; int ClassA::getA(){ return a; //<---…
TimeZero
  • 1
  • 1
-2
votes
2 answers

java hw. Abstract class with a running cash reserve, cash always returning to initialized number

I have an abstract class called Food where I initialize a double currentCash to 59. I have 2 subclasses of Food; Fruit and Meat where I subtract the price the user types in from my cash reserves of 59. After the user types in a price, then proceeds…
-2
votes
3 answers

Using a static variable of a shared library in more than one functions of a same exe, but different object file

(i have edited my original question to make it more understandable) here is the prototype for the problem .... //Txn.h ---this has a static variable, usable by the pgms including it. class Txn { public: static int i; static void…
-2
votes
2 answers

Static member functions that are const with respect to static variables

In C++, how do you declare a static member function of a class to be const with respect to the static member variables of that class? Consider the following simple example. myclass.h: class myclass { myclass() { myint = 0; } …
cmo
  • 3,762
  • 4
  • 36
  • 64
-3
votes
1 answer

How to initialize a static array of pointer to function method c++

I have a class that hold public static method corresponding to mathematical operations. The class is implemented like such class test { public: test(){}; ~test(){}; typedef int(test::*calcul)(int a,int b); …
toure
  • 1
  • 3
-3
votes
1 answer

How to access static variables from within object in Java?

From within the class, how do I access a static variable explicitly? (Is the best practice to access static variable explicitly eg. using static.staticVar) The below works class Something { protected static _var1; public void somefunc() { …
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
-3
votes
1 answer

c++ : cannot call member function without object

I am trying to write a class with a map to keep a registry with unique ID for later accessing the objects. All compiled fine till i wrote the for loop in main trying to access objects of the class and their data. I am at a loss what is exactly…
Stephane
  • 29
  • 1
  • 6
-3
votes
1 answer

Address of a static variable when instance created

I have a c++ class which has static const and static variable on it. During startup I initialize all the static const variables with some string values and all the static variables with zeros. Then I create 1st instance of that class and tried to…
kar
  • 495
  • 1
  • 8
  • 19
-3
votes
1 answer

Why aren't we allowed to access a private static member outside class without public member function?

This may be a silly doubt but I am not able to understand that why I am not able to access a private static data member outside the class when I am allowed to define it. For ex: in the following code: class foo { static int A; public: …
Rahul Jain
  • 67
  • 1
  • 2
  • 8
-3
votes
2 answers

Is this correct in c++

Is the following code is correct if I have it in a header file? template Stopwatch *Stopwatch::m_instance = nullptr; class Stopwatch { std::clock_t m_lastStep; std::clock_t m_start; static Stopwatch *m_instance; }; ok With the help…
mans
  • 17,104
  • 45
  • 172
  • 321
1 2 3
78
79