Advertisement

Obj Reference

Started by September 13, 2000 04:03 PM
1 comment, last by Blue*Omega 24 years, 3 months ago
Here''s and odd question that may or may not have an obvious answer. In my current project I want to make a list of any object I create for collision detection, rendering, bsp tree, etc. I have my object class which includes all the objects info such as verticies and a List class that looks somewhat like this. class List { public: Render(); Clear(); ... Add_Obj(3D_Obj name); Del_Obj(3D_Obj name); private: 3D_Obj *objlist; // list of pointers }; I want to be able to call the Add_Obj() function every time I call a 3D_Obj constructor. Add_Obj() will then add a pointer to the new object to the list. My problem now is that I need the 3D_Obj to reference its own name so I can send it to the list. I''ve heard of somthing called a "this" pointer, is that what it does? Please ask for clarification if I didn''t explain well enough. ----------------------------- Blue*Omega (Insert Witty Quote Here)
// Tojiart
Lets see.
the ''this'' pointer is simply a pointer to the class. It''s how the class can say, ''this is me, and where I am''
It works just like any other pointer, you can cast it to void* or whatever you need.

So in the constructor of your 3dobj you can call the add_obj function:
Add_Obj(this);

now your list will have a pointer to the object.
But, be careful, as soon as the object goes out of scope it will call its destructor and the list will point at useless spot.

There are ways around this, like creating dynamic objects. And having the list destruct them when it destructs.

If I was unclear or left out something just ask.

I think therefore I code.
Just because the church was wrong doesn't mean Galileo wasn't a heretic.It just means he was a heretic who was right.
Advertisement
Hi,

Why don''t you derive all your classes form a base CObject class. This object class has static member functions to store those informations. A static member function like CObject::Status()
could print all created classes. I found this works pretty good


Tim

--------------------------
glvelocity.gamedev.net
www.gamedev.net/hosted/glvelocity
Tim--------------------------glvelocity.gamedev.netwww.gamedev.net/hosted/glvelocity

This topic is closed to new replies.

Advertisement