Sign in to follow this  
Raeldor

How to Resolve Circular References

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this