Sharing data between classes

Started by
4 comments, last by Smorg123 21 years, 8 months ago
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.
Advertisement
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]

The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.

?Have a nice day!?

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 pDatareturn mc->Get();}    


Hope this helps,
Crispy

edit: a silly mistake...


[edited by - crispy on August 18, 2002 5:00:15 AM]
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
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.
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
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?)

This topic is closed to new replies.

Advertisement