Archived

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

Coding styles...

This topic is 5671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

okie, this is one puzzling matter i got. i am using the framework provided by gameinstitute.com, and heres what i have at the moment: //1)I have a derived & concrete class in crystal.h/cpp: class CrystalApp //and declared as CrystalApp theApp; 2)A class hierarchy exists in gameobjects.h file as: class GameObject | class CrystalMaster | classes Crystal_A,Crystal_B,PlayArea GameObject is the abstract base class, CrystalMaster is the abstract derived class, Crystal_A,B,and PlayArea are derived concrete classes. 3)in crystal.h, we declare these:
      
class CrystalApp :...
{...  
private:
  GameObject* object[8];
  GameObject* object1[200];
  Crystal_A* crystal_a[4];
  Crystal_B* crystal_b[4];
  PlayArea* playarea[10][20];
}
  
4)so in crystal.cpp, -ALL the arrays of pointers are allocated memory dynamically. -Each element of the arrays is assign to a GameObject pointer. example:
              
for(int i=0; i<4; i++)
{
object[i] = crystal_a[i];
}
  
-so most operations like Render,Update,Delete are using GameObject pointers. 5)my current problem: In order to utilise the pointers stated in CrystalApp class, i used friend classes. So most of the code in GameObject.cpp, are quite ugly, like this:
        
theApp.crystal_a[0]->.........
  
"theApp" has to be used all the time, coz the pointers belong to CrystalApp class in private access. 6)I am looking for a more elegant approach, one that does not compromise the easiness of doing all the memory allocation in CrystalApp class, and perhaps do not need to use "theApp" all the time. Heres what i thought of at the moment, changing crystal.h stuffz to:
              
class CrystalApp :...
{...  
private:
  GameObject* object[2];
  GameObject* object1;
  CrystalMaster* crystal[2];
  CrystalMaster* playarea;
}
  
and in CrystalMaster class,
              
class CrystalMaster : class GameObject
{
...
protected:
Crystal_A crystal_a[4];
Crystal_B crystal_b[4];
PlayArea playarea[10][20];
}
  
How do i actually link, lets say, crystal[0] to the array crystal_a[]?? considering that memory allocation was done like this in crystal.cpp:
              
crystal[0] = new CrystalMaster(...);
crystal[1] = new CrystalMaster(...);
playarea = new CrystalMaster(...);
  
Or is there something wrong with this new method? [edited by - edwinnie on June 2, 2002 9:16:31 AM]

Share this post


Link to post
Share on other sites
Use the manager concept - have a class that is responsible for managing collections of a specific type of entity (e.g. CrystalManager). The manager should be a single, global object so that all parts of the code have access to it. They (other portions of code) register any objects they create within the manager, indicating ownership (or transferring it to the manager - the simpler, more elegant approach), and access the created objects through the manager as well. This unifies and simplifies access, and creates clear cleanup responsibility.

Share this post


Link to post
Share on other sites