Sign in to follow this  
CJWR

correct way to use static

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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_H

class Ex {
// declare it
static int ex;
};

#endif

// ex.cc
#include "ex.h"

// define it
int 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Friend is just access. You could use public and let everything access it, but it would still have to return a pointer or a static or maybe a reference? The derived class makes a new instance of the base class, that's why you have to pass parameters when there are any.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this