Archived

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

Smorg123

Sharing data between classes

Recommended Posts

After searching the net for too long, I am hoping someone here can help me. If I have an (class) object that has data that needs to be loaded with it (eg. textures, model data), how can I reuse it without wasting time/resources reloading it. eg. if I had 5 cars that all looked the same, how could I do this with OO techniques and use the same data thanks for any help.

Share this post


Link to post
Share on other sites
Use pointers

Mycars *cars;
cars = new Mycars;
....
Mycars *anotherptr = cars;

result two pointers that point to the same class address so memory is not wasted

or just create pointer to your data

Mycars cars;
.......
Mycars *cars_ptr = & cars;


[edited by - GoofProg on August 18, 2002 4:51:27 AM]

Share this post


Link to post
Share on other sites
Not sure if this is what you had in mind, but here goes:


    
class MyContainer
{
public:
void* CarInfo;
};

class MyClass
{
private:
MyContainer* c;

public:
void BindContainer(MyContainer * pCont)
{
c = pCont;
}

void* Get()
{
return c->CarInfo;
}

};

void* func(void * pData)
{
MyClass* mc = new MyClass();
MyContainer* cont = new MyContainer();

mc->BindContainer(MyContainer);

cont->CarInfo = pData;

//now returns pData

return mc->Get();
}


Hope this helps,
Crispy

edit: a silly mistake...


[edited by - crispy on August 18, 2002 5:00:15 AM]

Share this post


Link to post
Share on other sites
Okay that looks pretty good.

To make an example of something like grand theft auto. All the cars (textures, width''s, height''s, etc) could be loaded into containers, then another instance of a class could be created each time a new car came along, that had variables of things that would change (eg, speed, direction) and it just binded the container information.

I hope I''m thinking along the right track.

Share this post


Link to post
Share on other sites
May I suggest taking a look at Smart Pointers, from the boost library? Otherwise, you''ll have to be very careful about memory management.

Cédric

Share this post


Link to post
Share on other sites
Or - just make your resource manager class a singleton - ie CTextureManager.

Your new class asks it to /make sure/ the resources it needs are loaded.

CTextureManager then checks its own lists/array (whatever) for the resource, if it isn''t present, it loads it. Each outcome returns a pointer to the resource.

In OpenGL - this approach is a godsend, because you can ask CTextureManager to reload all texture resources whenever a resolution change occurs, and all of your models will still be able to access their textures as if nothing happened. (Guess the same applies to DX?)

Share this post


Link to post
Share on other sites