简体   繁体   中英

how to manage multiple files, global variables, and defines

I have two classes, class "entity and class "tile".

Class "entity" has some member functions that uses a global array of type "tile". It also uses some defined numbers.

Class "tile" contains a member variable that's a pointer to type "entity".

I want to separate the classes into various .h files. I'm going to try and restructure it, but I do want to know if it's possible to do this.

So, again, for clarity:

"entity" uses global 2d array of type "tile"

"tile" uses

Is there any way to split this up into three .h files (one for each class, and one for all the global variables and defines)?

Thanks!

I don't see it why do you need three .h files. Just make a unit for each class, and put the global into Entity 's module (I wouldn't argue that you could possibly avoid globals).

Entity.h

class Entity
{
<...>
};

Entity.cpp

#include "Entity.h"
#include "Tile.h"

Tile array[100];//here's your array

Tile.h

#include "Entity.h"

class Tile
{
    <...>
    Entity * ptr;//here's your pointer
};

I think you just need a forward declare on class Entity?

tile.h:

class Entity;

class Tile {
     Entity *entity;
      ...
}

entity.h:

//#include "tile.h" - add this back if you need to refer to tile in Entity defn

class Entity {
    ...
}

entity.cpp

#include "entity.h"
// Remove the following or put in proper include protection if you uncomment the 
// include above
#include "tile.h"

Tile gbl[10][10];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM