Is it bad to avoid circular dependency like this?

Hey there , i have two classes , and unfortunatelly, I made it in a way where they both have to include each other. Rookie mistake i know :confused: , but i managed to ,bypass the issue by including one of the header files in the cpp file.

I’m pretty sure its not the best method to do this, and next time i should write the code , in a better way, but I just wonder how bad this method is. Can i face problems in the future with this?

It is a really bad practice and should not be done. There is a diamond dependency problem in programming, diamond dependency can be managed, but it is usually avoided. I guess you would not get any problems, if you really manage these dependencies, but in a long term, it would be a headache.

Include the needed header class (of other class) in the cpp file and in the header file just use forward declarations.

in cpp

#include "Components/BoxComponent.h"

in header

//# normal class def then properties

UPROPERTY()
class UBoxComponent * myBoxCollider;

Writing UBoxComponent * myBoxCollider; wthout class in front would throw an error.

You could also forward declare the class at the top of the file if you will use many instances of it.

1 Like

Yeah i thought so, im still pretty new to the c++ it self. but im trying to learn , and make sure i dont commit any programming crimes :smiley:

Including , the headerfile in cpp, and forward declaring the class is how i did it

You could also try to decouple the two classes through the use of interfaces.

Thank you! I will check it out :slight_smile: