Archived

This topic is now archived and is closed to further replies.

papa

non-static member used in static member function

Recommended Posts

Hi all. I have a class which has one static member function. class foo { public: static void compute(); private: int dimension; // it should be non-static } // static function void foo::compute() { // I cant use dimension var here as it is not static data member. // what can I do in order to use it here? } Actually can somebody correct me if I am wrong. I've read some stuff on static data members, functions and classes and as far I know it creates only one data member or function or object (if its a class) in memory no matter how many objects are created of the same type. Say I have a class CObject which has one static data member called Size=10. Now if I create 20 objects in my game all of them will have just one instance of Size=10 held in memory. Am I right. Even if I try to set the size var in each object to a different size it wont allow m? Am I right or it will but will conmtain the last initialized value? Does it work the same for functions and classes. Could somebody explain this please. Best in a small code example or pseudo-code. Thank you. [edited by - papa on April 5, 2004 9:53:30 AM]

Share this post


Link to post
Share on other sites
Because your foo::compute function is static it doesn''t know about any member variables. You need to get a pointer or reference to a ''foo'' object so it know which object to use. You could change your compute function thusly...

static void compute( foo& FooObject );

Share this post


Link to post
Share on other sites
and if you need to pass a reference to an object of the class to a static method in the same class then you need to question why the method is static in the firstplace. The difference between a static and non-static method is that the static does not have an implicit "this" pointer - ie an reference to the object. If you explicitly pass a reference to the object then why make it static?

Share this post


Link to post
Share on other sites
quote:
Original post by papa

Actually can somebody correct me if I am wrong.
I''ve read some stuff on static data members, functions and classes and as far I know it creates only one data member or function or object (if its a class) in memory no matter how many objects are created of the same type. Say I have a class CObject which has one static data member called Size=10. Now if I create 20 objects in my game all of them will have just one instance of Size=10 held in memory. Am I right. Even if I try to set the size var in each object to a different size it wont allow m? Am I right or it will but will conmtain the last initialized value? Does it work the same for functions and classes. Could somebody explain this please. Best in a small code example or pseudo-code. Thank you.



If you make a variable static, then there is only one copy in memory. All CObjects can access and modify it, if you make any changes to your ''Size'' variable all CObjects will be able to ''see'' it.

Hope that helps.

Share this post


Link to post
Share on other sites
ok. thank you.
I know that this is a bit strange way of doing things. I actually downloaded some code and trying to understand it.

I have a class which has a private constructor and no destructor.

Every time the object is created one calls function CreateObj which is static member function.

the syntax is:


struct Object
{
public:
int TriCount;
int Size;
Triangle **Triangles;
Draw()
static Object* CreateObj(int size, int triCount);

private:
Object(){}
}


Object* Object::CreateObj(int size, int triCount)
{
Object*obj = (Object*)new uchar[size];
memset(obj , 0, size);
obj->Size = size;
obj->TriCount= triCount;
uchar*ptr = (uchar*)obj+sizeof(Object);
obj->Triangles = (Triangle**)ptr;
ptr += sizeof(Triangle*)*obj->TriCount;
obj->Triangles[0] = (Triangle*)ptr;
return obj;
}


main()
{
Object *Obj = NULL;
obj = Object::CreateObj( size, trianglesCount);
............
delete Obj;

}

Why not have just a simple constructor rather than this messy code. Arghhh...

So. When do you guys suggest to use static members or functions or classes. Actually is there any point to use static classes? if we only see only one object even though there could be 50 of them but because they''re static only one is in memory. I am a bit confused.....

Share this post


Link to post
Share on other sites

quote:
Original post by papa
Why not have just a simple constructor rather than this messy code. Arghhh...

So. When do you guys suggest to use static members or functions or classes. Actually is there any point to use static classes? if we only see only one object even though there could be 50 of them but because they''re static only one is in memory. I am a bit confused.....


It looks like the author is trying to create an arbitrary sized object with n Triangles. To be honest, I think there are better ways of going about that - but maybe there are some requirements that aren''t obvious from the code.

Share this post


Link to post
Share on other sites