Am I Overcomplicating My C++ Class Design for a Game Engine?

0
3
Asked By CuriousCoder123 On

Hey, I'm new to C++ and I've been heavily using classes in my coding, especially while developing a game engine library. I created an asset loading system with an interface and specific classes for different types of assets. For instance, I have an interface called `IAssetLoader` with classes like `MeshLoader` and `TextureLoader`. Here's my code snippet for reference:

```cpp
class IAssetLoader {
public:
virtual ~IAssetLoader() = default;
virtual std::unique_ptr load(const AssetMetadata& metadata) = 0;
};

class MeshLoader : public IAssetLoader {
public:
MeshLoader(IGraphicsDevice* graphicsDevice);
std::unique_ptr load(const AssetMetadata& metadata) override;
private:
IGraphicsDevice* m_graphicsDevice;
};

class TextureLoader : public IAssetLoader {
...
};
```

Looking at my setup, I'm starting to think that I might have overthought this. It seems like I may not need so many classes since I probably won't add many more types of loaders, and their functionality is mainly to load assets through a single method. I also just realized that the only reason for using polymorphism is for a small unordered map of loaders. Could I just convert this to simple functions like `loadMesh()` and `loadTexture()` or maybe use templates instead? What would I gain or lose by relying on free functions over runtime polymorphism? Just want to know if I'm overcomplicating things!

1 Answer

Answered By DevGuru42 On

Honestly, it's more of a design preference. Interfaces and polymorphism are great for unit testing, but if you foresee only a couple of loaders, they might be unnecessary. As you mentioned, simplifying to functions could make your code cleaner and easier to manage. Just remember, if you ever need more flexibility later, you can always refactor to add classes down the line, so starting simple is often better.

Overthinker99 -

Got it! That makes a lot of sense. So what about when you *do* need inheritance? How do I figure out if my design should require a base class?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.