How to Resolve Circular References

Started by
2 comments, last by GameDev.net 18 years, 11 months ago
I have two classes, they both use pointers to each other. Each of the classes is definied in it's own header file for the module. How can I resolve this circular reference?
Advertisement
If the classes are ClassA and ClassB, right before the declaration of ClassA, put:

class ClassB;

This won't work if you do anything more than declare a pointer to it.
Thanks very much.
My suggestion would be to refactor your code to get rid of the cross-dependencies. There are several ways to accomplish this, I recommend reading a book about design patterns, or at least use google.

One way would be to break out an interface that both classes knows about (both classes #includes "MyInterface.h"), class A would implement (inherit) that interface, and class B would have an interface pointer to class A that it calls methods in.

This topic is closed to new replies.

Advertisement