Share a class instance between many other class instances

Share a class instance between many other class instances

Post by Matt Ros » Tue, 07 Jun 2005 18:38:46


I need to have one instance of a class called TGrid shared amongst my main
BCB class and also many instances of classes called TAgent and TFood. The
way I understand it I should be using extern to accomplish this.

In my TGrid header I declare the instantiation as:

TGrid theGrid;

In the main, TAgent and TFood class headers I have:

extern TGrid theGrid;

I also make sure each of these has 'TGrid.h' included.

This compiles OK but for some reason the main class does not seem to share
the data in theGrid with the other classes. This class represent a
2-dimensional grid (x,y) of TList objects representing items on a grid. If I
add items via TAgent they don't show up if I later read them in the main
class.

I have also tried using a static instance of TGrid instead of extern, and
that had similar results.

Please can somebody advise me on what I'm doing wrong?

Thanks,

--
Matt Ross

-=I haven't lost my mind; it's backed up on tape.=-
 
 
 

Share a class instance between many other class instances

Post by Hans Galem » Tue, 07 Jun 2005 18:52:51


That is a possibility.

No. In TGrid.h you should write :

extern TGrid theGrid.


No. Remove those.


That is maybe correct. But better include TGrid.h in the .cpp files
wher you want to use theGrid.

Hans.

 
 
 

Share a class instance between many other class instances

Post by Hans Galem » Tue, 07 Jun 2005 18:56:58


And in TGrid.cpp:

TGrid theGrid;

Hans.