Extract all related class type aliasing and enum into one file or not

Posted by Chen OT on Programmers See other posts from Programmers or by Chen OT
Published on 2014-05-20T07:31:27Z Indexed on 2014/06/08 21:40 UTC
Read the original article Hit count: 221

Filed under:

I have many models in my project, and some other classes just need the class declaration and pointer type aliasing. It does not need to know the class definition, so I don't want to include the model header file. I extract all the model's declaration into one file to let every classes reference one file.

model_forward.h

class Cat;
typedef std::shared_ptr<Cat> CatPointerType;
typedef std::shared_ptr<const Cat> CatConstPointerType;

class Dog;
typedef std::shared_ptr<Dog> DogPointerType;
typedef std::shared_ptr<const Dog> DogConstPointerType;


class Fish;
typedef std::shared_ptr<Fish> FishPointerType;
typedef std::shared_ptr<const Fish> FishConstPointerType;

enum CatType{RED_CAT, YELLOW_CAT, GREEN_CAT, PURPLE_CAT}

enum DogType{HATE_CAT_DOG, HUSKY, GOLDEN_RETRIEVER}

enum FishType{SHARK, OCTOPUS, SALMON}

Is it acceptable practice? Should I make every unit, which needs a class declaration, depends on one file? Does it cause high coupling?

Or I should put these pointer type aliasing and enum definition inside the class back?

cat.h

class Cat
{
    typedef std::shared_ptr<Cat> PointerType;
    typedef std::shared_ptr<const Cat> ConstPointerType;

    enum Type{RED_CAT, YELLOW_CAT, GREEN_CAT, PURPLE_CAT}
    ...
};

dog.h

class Dog
{
    typedef std::shared_ptr<Dog> PointerType;
    typedef std::shared_ptr<const Dog> ConstPointerType;

    enum Type{HATE_CAT_DOG, HUSKY, GOLDEN_RETRIEVER}
    ...
}

fish.h

class Fish
{ ... };

Any suggestion will be helpful.

© Programmers or respective owner

Related posts about c++