Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have many models in my project, and some other classes just need the class dec

ID: 658527 • Letter: I

Question

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.

Explanation / Answer

Having a single file with all your class declarations seems like asking for trouble to me. There are architectural as well as tooling based reasons for my statement.

Let's look at tooling first. Most compilers can generate partially compiled files (such as .pch) based on which header files are included. These files are recreated only when those header files change. For large enterprise projects, this can mean the difference between a 5 min build and a 1 hour build. If you put all your declarations in one file, adding a single class will trigger a full build - not too desirable. There are intense debates around whether to use pre-compiled files in the first place, but this point is definitely something to keep in mind.

Many tools such as IDEs also use header scope to determine the level of auto-complete, etc. With a global header file, I've seen IDEs slow to a crawl as they try to process the massive include tree.

Secondly, as your project grows and you start using hundreds of classes, having them all declared in a single file will cause a lot of unnecessary coupling. The whole point of header files is to specify the compilation/lookup scope for your objects. It is better for each class to know only what it strictly needs. This enables future modularization and extensibility.