correct way to use static

Started by
9 comments, last by fireside 18 years, 10 months ago
alright, i have a class (alien) which has a few images in it. now, in my game, i have an array of this class. now i know that static sort of links the data members in a class so that every such object shares that data member's data. now because all of these aliens in my game have the same images. i want to use static to save memory. how do i go about doing this? because the following doesn't work (in header file) static IMAGE alien_gra; IMAGE is a class i created btw which does work, right now my game works. but because i have that piece of code without the static: IMAGE alien_gra; i'm using far more memory than i need to. so how do i use static correctly? i'm near sure this is how we were taught to use static in my c++ classes in collage. i'm using vc++ 6.0 if it matters. thank you for any help you can give me.
Charles Reed, CEO of CJWR Software LLC
Advertisement
Well, we need more information.

static is indeed a member that is shared by all the instance of a particular class. Seems like you are using it the right way, however many issues and code changes might have to be done, depending on the usage of the member within and outside the class.

Some code and the compile errors generated would be useful.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
There are two things you need to do: first you need to declare the variable to be static (i.e. there is just one instance of it), second you need to define it somewhere. The declaration is found in your header file (inside the class declaration), the definition is usually found in a source file (because the linker will complain if you define it multiple times).

Example:
// ex.h#ifndef EX_H#define EX_Hclass Ex {// declare itstatic int ex;};#endif// ex.cc#include "ex.h"// define itint Ex::ex;


BTW you should not worry about the additional memory required if you don't make it static. But make sure that your class stores a pointer/reference to the image.

Often it is better to avoid static members if possible.
Quote:Original post by nmi
Often it is better to avoid static members if possible.


What ?
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
you might want to develop a manager of some sort, for example all of your alien use the image alien.tga, then the manager will keep that image and for all the alien will have reference to that image that the manager keeps. The manager's job is to have only one unique copy, if the image already exist then it will return a reference/pointer to the image/resource.
Quote:Original post by xMcBaiNx
Quote:Original post by nmi
Often it is better to avoid static members if possible.


What ?


Uh .. yeah .. what ?
I think static members are going to hang around for the duration of the program, right? Where shared pointers will eventually take it out of memory when there are none left.
The idea with a manager class sounds nice and maybe I'll give it a try in my next project.
In my current project, every class has a private static Object, that holds all static data, so inherited classes can have their own static data without interfering with the parent class' static data.
To initialize the static data I'm using a static setup function that's called by the classes' factory class, in its constructor, and objects are created only through this factory class so I can always be sure that the static data is ready.

The values of the static data is stored in a file, so I can make a lot of changes in the behaviour of each class without recompiling, of course a manager class can do the same.

I'm just questioning me what is better, my way or the manager class...
hmm, thank you for the infomation, i think i'll just leave it the way it is. the game only uses about 2mb of ram when it is runnin the way it is. I'll work more on my game programming skills and worry about static and stuff later on.
Charles Reed, CEO of CJWR Software LLC
I have a question now!!! Instead of using static why not just create a freind function or since all the alliens have the same pic why not use derived classes so they can use the image of the base class?
Computers are worlds of Exploration?

This topic is closed to new replies.

Advertisement