solving circular dependencies with class only delcared in .h

M

Mosfet

Hi,

I have two classes, let's call them class A and class B with mutual
dependencies as shown below and where implementation is inside .h (no cpp)


#include "classB.h"
class A
{

};

#include "classA.h"
class B
{
};

So to solve this I am using forward declaration in class B and I had
to mo ve implementation in a .cpp

class A;
class B
{
...
};


IS there another way to solve this and to keep implementation only in .h ?
 
J

Juha Nieminen

Mosfet said:
So to solve this I am using forward declaration in class B and I had
to mo ve implementation in a .cpp

class A;
class B
{
...
};


IS there another way to solve this and to keep implementation only in .h ?

Remove the circular dependency?

Seriously, though, that *is* the kosher way of resolving circular
dependencies. There's no better way (besides redesigning your program to
remove the circular dependency in the first place, of course).

In fact, it's a good custom to always prefer forward declarations in
header files whenever possible, rather than #including more header files
(even if there wasn't any mandatory technical reason to do that).
Reducing include dependencies has many benefits.
 
S

Salt_Peter

Hi,

I have two classes, let's call them class A and class B with mutual
dependencies as shown below and where implementation is inside .h (no cpp)

#include "classB.h"
class A
{

};

#include "classA.h"
class B
{

};

So to solve this I am using forward declaration in class B and I had
to mo ve implementation in a .cpp

class A;
class B
{
   ...

};

IS there another way to solve this and to keep implementation only in .h ?

You've not shown enough info.
Solving mutual dependencies depends on whether you have a circular
dependancy, which may not be finite.

what if A has_a B which in turn has_an A which has_a B ... infintely?
 
M

Marcel Müller

Mosfet said:
IS there another way to solve this and to keep implementation only in .h ?

Why do you want to have the implementation in .h. This requires at least
that all of your functions are declared inline (implicitely or
explicitely). If the compiler cares about that is another question, but
at least it blows up the compile time of your project significantly.

If you have a cyclic dependancy of the /declaration/ of your classes
e.g. because of the use of certain smart pointers like intrusive_ptr,
then you have a serious problem. If only the implementaions depend on
each other you have no problem.


Marcel
 
J

Juha Nieminen

Marcel said:
If you have a cyclic dependancy of the /declaration/ of your classes
e.g. because of the use of certain smart pointers like intrusive_ptr,
then you have a serious problem. If only the implementaions depend on
each other you have no problem.

Actually if you have a circular reference with reference-counting
smart pointers, you do have a problem already.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top