static object

Dear Experts,

what is the purpose of declaring a C++ object as static.
when to declare an object as static.
Please provide a simple example along with explaination.

Thanks.
Ravikumar
A simple search for "C++ Static Object" is a lot faster than posting here.

Top hit:
http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx

closed account (D80DSL3A)
That snarky response seems a bit rough! MSDN is great but I often find it hard to learn much about the hows and whys of use from it.
I've found static data members in classes to be very useful in providing an interface between class member functions and global variables. Without them, in order to make data available for use in a member function I would have to either pass it (or a pointer to it) as an argument in a function call OR make it a data member of the class then keep every instances copy of the value updated. The first (passing as argument) makes writing virtual functions a nightmare. This is because all such functions must have identical parameter lists - I have to pass all data to each that any one might need. The second method (adding data members) is often wasteful because I'm creating many copies of the same value.
Example: In a space shooter video game several enemy ship classes are derived from a base ship class. Some ships fire aimed shots, others don't. Some ships fire different types of shots. I would like to have a virtual function for firing shots which can be called like this:
pShip->fire() where pShip may be pointing to an instance of any ship class.
The fire() needs a pointer to an array of shots for firing, the size of this array, an instance of a shot to serve as a template for initializing the fired shots data members and maybe the targets coordinates. All virtual versions of fire() have access to all this as common data because of static data members in the base class:
1
2
3
4
5
6
7
8
9
10
11
12
13
class ship
{
	// static dms
	static cvShot* p_cvDump;// pointer to ammo dump (array of constant velocity shots)
	static int cvDumpsz;// size of this array
	static float tposx;// target position maintained under WM_MOUSEMOVE
	static float tposy;
	static cvShot* p_cvTemplates;// templates for fired shots
        static int NcvTemplates;// size of template array
        static int playerScore;// a member function can update this value when a ship is killed
	static int numKills;// this one too
	// more stuff
}               

Those last two (playerScore, numKills) enable simplification of the game logic. When the function
pShip->check4Hit() determines a kill has been made, these values are automatically updated like so:
1
2
3
4
5
6
7
8
9
10
ship::check4Hit()
{
      // a bunch of other code
        if( hitsLeft <= 0 )// that was a kill
        {
		inPlay = FALSE;
		playerScore += scoreValue;// static
		numKills += 1;// ship class variables
	}
}


I hope the examples given were sensible.
hi,
i want to ask can i have a static class and function? like..
static class test{};
or
static void blabla(){}
can i have something like this?
the what the use of it?
static functions of a class in C++ can be called without the need of an instantiated object of that class. Is that what you want? Yes, you can do that in C++. What is a static class in Java?

EDIT: Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;

class MyClass
{
public:

    void member_func()
    {
        cout << "hi! I'm a member ";
        cout << "function!" << endl;
    }

    static void static_func()
    {
        cout << "hi! I'm a static ";
        cout << "function!" << endl;
    }
};

int main()
{
    MyClass my_object;

    my_object.member_func();
    my_object.static_func();

    //This would generate
    //a compilation error
    //MyClass::member_func();

    MyClass::static_func();

    cin.get();
    return 0;
}
Last edited on
oh..thanks....
there is no static class in c++? just wondering =(
Last edited on
All internal classes are already "static" in the way Java uses them:

1
2
3
4
5
6
7
8
9
10
class Outside {
    class Inside {
        int a;
    };
    int b;
};

int main() {
    Outside::Inside test;
}
I did some searching on my own to find out what a static class in Java is. C++ doesn't directly provide a mechanism to make a class static, but you can do it yourself and it's not difficult. Take a look at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;

class MyStaticClass
{
public:

    static void SetValue(int n)
    {
        value=n;
    }

    static int GetValue()
    {
        return value;
    }

private:

    static int value;

    //private contructor/destructor
    //to prohibit instantiation
    MyStaticClass(){}
    ~MyStaticClass(){}
};

int MyStaticClass::value;

int main()
{
    //This generates an error,
    //because the constructor
    //of MyStaticClass is private
    //MyStaticClass my_object;

    MyStaticClass::SetValue(10);
    cout << MyStaticClass::GetValue();
    cout << endl;

    cin.get();
    return 0;
}
thanks alot for the information....
appreciate it
Topic archived. No new replies allowed.