0

I have a game class as follows:

// game.h
class Game {
 public:
  void Run();
  void CleanUp();
}

I want to create a texture pointer which is declared here, but initiated at construction. This pointer is to a type that exists in a third party header file:

private:
 std::unique_ptr<Texture> sprite_sheet; // Texture is in game_engine.h which is third party.

The problem is, if I #include "game_engine.h" in this header, then every file that includes this header will include everything from game_engine as well which I want to avoid. Ideally, I would only like to include game_engine in the source file (.cpp).

Is there a standard design pattern to help me avoid this?

One way is to create my own Texture class which only exposes the relevant parts I want. But this will slowly blow out of proportion as I will have to re-do my own classes for everything.

Plasty Grove
  • 2,807
  • 5
  • 31
  • 42
  • You might be looking for the pimple idiom. – YSC Nov 21 '20 at 23:20
  • 1
    The term for further research is "forward declaration". Add this line before `class Game {` in the header file: `class Texture;` – Igor Tandetnik Nov 21 '20 at 23:20
  • Why don’t you want the header file included in other source files? – Sneftel Nov 21 '20 at 23:20
  • @IgorTandetnik - THanks, I tried it with it, but I am getting an error - `Use of undefined type Texture`. `Texture` is a `struct` in the third party library, would that make a difference? – Plasty Grove Nov 21 '20 at 23:33
  • @YSC - THanks, that looks similar to forward declaration. Unfortunately, I'm getting the same error. – Plasty Grove Nov 21 '20 at 23:34
  • Is it in a namespace, by any chance? – Igor Tandetnik Nov 21 '20 at 23:34
  • @Sneftel - It seems messy. I want usage of third party code to be in one place rather than sprinkled all through my code. – Plasty Grove Nov 21 '20 at 23:34
  • @IgorTandetnik - Nope, it's a C library though, could that be a problem? very simple struct. – Plasty Grove Nov 21 '20 at 23:36
  • 1
    On what line of code do you get "use of undefined type Texture" error? [This code compiles](https://godbolt.org/z/57sxoq); to the extent there is a problem, it's necessarily in the code not shown. – Igor Tandetnik Nov 21 '20 at 23:38
  • @IgorTandetnik - Okay it seems to work if I declare it as a raw pointer and not as a unique pointer. Now I can forward declare it. I guess it makes sense, because the engine keeps track of the textures, I shouldn't be the one owning them :). If you could post the answer, I am happy to accept it! – Plasty Grove Nov 21 '20 at 23:44

1 Answers1

0

Posting an answer because there's a bit more for completeness. Here is what worked for me.

The source file is as follows:

// game_engine.h - C library.
typedef struct Texture {
  ...
} Texture;

If I want to use a unique_ptr, the approach is as follows:

// game.h
// Forward declare the typedef.
// https://stackoverflow.com/q/804894/1287554
typedef struct Texture Texture_;

class Game {
 public:
  void Run();
  void CleanUp();
  
  // Destructor is required for unique_ptr and forward declarations.
  // https://stackoverflow.com/q/13414652/1287554
  ~Game();
 protected:
  std::unique_ptr<Texture_> sprite_sheet;

};


// game.cpp
#include "game.h" // This is first.
#include "game_engine.h"

void Game::Run() {
  sprite_sheet = std::make_unique<Texture_>(LoadTexture(...));
}

// This has to be explicit.
Game::~Game() = default;
Plasty Grove
  • 2,807
  • 5
  • 31
  • 42