Static member data...trouble

Started by
7 comments, last by RZ 22 years, 6 months ago
Hi I have a problem that I cant solve in a nice way.I have a class COctree and this class includes data members: static CVertex *VertexData COctreeNode Node (and more) Now the thing is that I want the COctreeNodes Node to be able to read from VertexData in class COctree. I thought I make the VertexData static and so that I can read VertexData like this: float x = COctree::VertexData->x But you can't do that(well I cant)... Because static data member needs to be "explicitly define". I know how to do this if i have a static data member looking like this: static int sum...but not when its a pointer. If anyone have a clue what im talking about and have some suggestion on how I can solve this I'll be glad to hear Edited by - RZ on October 10, 2001 9:13:59 PM
Advertisement
A static data member belongs to all instances of a class (they all share one copy), so the data memeber must be intialized on a class basis:
// SomeClass.hclass SomeClass{private:  static int data;};// SomeClass.cppint SomeClass::data = 0; 

The intialization doesn''t have to be in the implementation file; it can be in the header. Objects will now be free to access and modify data (and they don''t have to prefix it with the class name since they are instances of the class), but if one object changes the data and another accesses it, it will receive the most recent value. In other words, only use static data members if you want all of your objects to share that data.
yes i know that.. but how do you do it when the static data member looks like this:

static CVertex* VertexData...

if it was a normal data member I can write VertexData = new CVertex[NumOfVertices]...
But I dont know how do to this with a static CVertex* VertexData
CVertex COctree::VertexData = new CVertex[NumOfVertices];

NumOfVertices must be a constant.
Have you tried usinf friend classes?

  class COctreeNode{    ...};class COctree{public:     ...private:     CVertex      *VertexData;     COctreeNode  Node;     friend class COctreeNode;};  


-------------------------------
Did someone say "banana"?

..-=gLaDiAtOr=-..
Thanks
I was afraid that it had to be a constant ..thats not good

It works if I write:
CVertex* COctree::VertexData = new CVertex[NumOfVertices];
But it crash when I exit the program... Do u know how u delete this static pointer? If it is a static I can just delete it in the destructor because there may be (not im my case) other COctree objects left.

Gladiator: yes im using that..but that doesnt help me to get the COctree::VertexData from within a COctreeNode function...that only work if there is only one VertexData shared by all COctree objects.
Add a reference counter:
class COctree{private:  static CVertex *VertexData;  static int nRef;};CVertex *COctree::VertexData = new CVertex[NumOfVertices];int COctree::nRef = 0; 

Now, in the constructor and destructor:
COctree::COctree(){  // do all your regular initialization  ++nRef;}//COctree::~COctree(){  // do all your regular deinitialization  // nRef gets decremented before the test  if(--nRef) // if nRef != 0 we still have active COctrees    delete VertexData;} 


Now if you''re not content with the size of VertexData, any object can modify its size:
void COctree::ResizeVertexData(int size){  CVertex *pData = new CVertex[size];  // copy data in VertexData to pData  memcpy(pData, VertexData, sizeof(CVertex) * NumOfVertices);  delete VertexData;  VertexData = pData;} 

It might be wise to save the size of the current VertexData so you can resize more than once (replace NumOfVertices above with the saved size).
To Oluseyi:
There is no need in string
int COctree::nRef = 0;
because static members are initialized with zeros by default.
Thanks Oluseyi!!

This topic is closed to new replies.

Advertisement