Two Headers Using Each Other

One of my early frustrations  was when I couldn’t make two headers include each other, but after a while, I actually figured out that you can ‘trick’ the compiler to do so! Heres how:

You have two headers, Mesh and Shader, and the Mesh uses some of Shader’s stuff, and Shader uses a reference of Mesh. In Mesh you include Shader.h, and use it from there, but in Shader.h, you don’t include Mesh.h, instead you make a Class Prototype so you can use the class name freely, and then in Mesh.cpp, you then include Mesh.h and do stuff with it. Now the code version:

#include "Shader.h"

class Mesh
    void Create(ShaderProperty *a);


#include "Mesh.h"

Mesh::Create(ShaderProperty *a)


class Mesh;
class Shader
 Mesh* stored;

 void SetStored(Mesh* ref);


#include "Shader.h"

#include "Mesh.h"

Shader::SetStored(Mesh* ref)
 stored = ref;;

I just though I would share this tip to you guys, as it could become useful!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s