I have two classes with separate headers: class Renderer
and class Texture
. The Texture
instance is designed to manage some data that resides in the memory pool of a Renderer
, and thus a Texture
instance cannot exist independently of a Renderer
instance. To get a handle to a Texture
instance, one simply calls Renderer::loadTexture
with the correct filename, and Renderer
searches it's database for Texture
objects with a matching filename and instantiates a new texture if no matches are found.
//renderer.h
#include "texture.h"
class renderer
{
public:
Texture* loadTexture(std::string fileName);
private:
std::map<std::string, Texture*> textureDb;
};
//texture.h
class Texture
{
public:
Texture(std::string imageFileName);
};
It occurred to me that it would be logical to make the constructors of class Texture
private, and declare Texture * Renderer::loadTexture(std::string filename)
as a friend of class Renderer
:
//renderer.h
#include "texture.h"
class renderer
{
public:
Texture* loadTexture(std::string fileName);
private:
std::map<std::string, Texture*> textureDb;
};
//texture.h
class texture;
#include "renderer.h"
class Texture
{
private:
Texture(std::string imageFileName);
Texture(const Texture &);
friend Texture* loadTexture(std::string);
};
However, this only compiles if texture.h is always included before including renderer.h, since class Texture
requires class Renderer
to be already defined. What would be the best way to prevent this? Include guards are present on both files but are omitted here for brevity.