Jump to content
  • Advertisement
Sign in to follow this  
OpenGL_Guru

Static Class Variables vs Dynamic Class Variables.??

This topic is 4774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all i have looked and googled and its still unclear to me about class variables, specfically dynamic class variables. i have asked this question before but i realize i was probably unclear and now ive done more research on it. i need to create a dynamic class variable within a class but is shared by all the instances of the class. so in my declaration of the class i have: //class.h float *x; float *y; in the class definition i need to allocate memory for these but only once..that is the values these dynamic arrays will hold needs to be shared with every instance of the class. this will be more effecient as i dont need to allocate the same memory with the same values every time i want to declare a new object of the class. i also realize that i need to initialize these outside of the class to make sure new memory is not allocated with each new instance. however there is another dynamic class declared as: float *data; this will hold different data with every instance of the class and therefore is fine but it is dependant on the 2 dynamic class variables as: data = new float[sizeof_x * sizeof_y]; //a close example so this needs to be in the class itself but after the dynamic class variables have been initialized. anyway i guess i am wondering how to do this..i have tried looking for examples but everything seems to be about static class variables. appreciate your help in advance!

Share this post


Link to post
Share on other sites
Advertisement
class someclass
{
private:
/*
* x and y are now shared by all instances and you need to initialize them at file scope
*/
static float *x;
static float *y;
}

float* someclass::x = NULL;
float* someclass::y = NULL;

Share this post


Link to post
Share on other sites
The only variables that can be accessed by all instances of a class are either global variables or static variables. So you could just make x and y static variables in the class and make sure they are initialized before any of the objects are created. I also think x and y shouldn't be pointers.

Here's how I'd do it:

//foo.h
class CFoo {
protected:
static float x;
static float y;

public:
static void InitFoo();
}

//foo.cpp
#include "foo.h"

float CFoo::x;
float CFoo::y;

void CFoo::InitFoo() {
//calc x and y here.
}



But unless x and y are very expensive to calculate there is probably a better way of generating them. For example, if CFoo is created in batches, then the function that creates CFoo can calculate x and y and pass it as a parameter to Foo's constructor. Or the object that creates CFoo can calc and store Foo's x and y when it is created, and juts pass it onto the foos when it creates them.

Share this post


Link to post
Share on other sites
thanks for your help...

they will have to be pointers as i will need to perform a new on them to allocate memory ???

also the variables need to be public since i will be directly accessing them from its parent class. the reason i am doing this is to save even more memory. i.e. in my display list when i am drawing my triangle strips i can do something like:

glVertex3f(parent_class->x[index], parent_class->y[index], z); //like this

basically main creates an instance of parent_class. then parent_class creates an instance of the child_class which is where i need to declare/initialize my global/static variables. parent_class as explained above needs to be able to access these specific variables directly.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!