• Advertisement
Sign in to follow this  

How to Resolve Circular References

This topic is 4622 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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
Sign in to follow this  

  • Advertisement