0

In the following example, the FileA header is included in FileB header; Some of the classes in FileB.h are forwardly declared inside FileA.h;

in FileA.h

//Forward Declaration.
class classB;

//Main classes.
class ClassA
{
//...
private:
void MemberFunction(ClassB* PtrToClassBObj);
}

In FileB.h

//Includes.
#include FileA.h

//Main classes.
class ClassB
{
//...
public:
int MemberVariable = 0;
}

Then in FileA.cpp

#include FileA.h

//Member functions definitions.
void ClassA::MemberFunction(ClassB* PtrToClassBObj)
{
if(PtrToClassBObj)
PtrToClassBObj->MemberVariable++;
}

Is that enough to make FileA.cpp capable of accessing public members of said classes from FileB.h? or should FileA.cpp itself include FileB.h? And was the forward declaration needed (assuming no use of the class name inside FileA.h, and only used inside FileA.cpp exclusively)? What is the general best practice advice if any?

Physician
  • 483
  • 2
  • 7
  • 2
    FileA.cpp should include FileB.h. This question should explain what you need to resolve the circular header dependency: [https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm Nov 21 '21 at 19:19
  • Since `FileB.h` includes `FileA.h` and you're using a member of `ClassB`, you should be able to just include `FileB.h` – jkb Nov 21 '21 at 19:19
  • FileB.h does not use ClassA at all, and as such should not be including FileA.h. Only include what is actually used. Keep your includes as minimal and isolated as possible. – Remy Lebeau Nov 22 '21 at 09:15
  • @RemyLebeau Nope, it's including it to help frame the context of the question, obviously. – Physician Nov 22 '21 at 17:15
  • @Physician perhaps, but what I described is better practice than what the OP is asking for. – Remy Lebeau Nov 22 '21 at 17:39

1 Answers1

1

Is that enough to make FileA.cpp capable of accessing public members of said classes from FileB.h?

No, it is not "enough" - the full definition of ClassB needs to be visible.

or should FileA.cpp itself include FileB.h?

It should, because it uses the class at PtrToClassBObj->MemberVariable.

was the forward declaration needed

As presented, no - you can remove #include FileA.h from FileB and make FileA include FileB.

(assuming no use of the class name inside FileA.h, and only used inside FileA.cpp exclusively)?

Assuming there is no use of ClassB inside FileA.h, there is no use for forward declaring ClassB, so the forward declaration is not needed. The assumption conflicts with presented code, as the symbol ClassB is used in ClassA::MemberFunction declaration.

What is the general best practice advice if any?

Follow code guidelines. Do not write spaghetti code and do not have circular dependencies. Prefer clear, easy-to-understand, readable code structured in a hierarchical tree. Compile with all warnings enabled, use code linters and sanitizers. Exercise regularly. Eat healthy.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111