Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to deal with co-dependent classes in C++?

Tags:

c++

header

Say I have a class foo with an object of class bar as a member

class foo
{
    bar m_bar;
};

Now suppose bar needs to keep track of the foo that owns it

class bar
{
    foo * m_pfoo;
}

The two classes reference each other and without a forward declaration, will not compile. So adding this line before foo's declaration solves that problem

class bar;

Now, here is the problem - when writing the header files, each header depends on the other: foo.h needs the definitions in bar.h and vice-versa. What is the proper way of dealing with this?

like image 608
Nathan Osman Avatar asked Jan 27 '10 00:01

Nathan Osman


2 Answers

You need to move all of the member access out of the header, and into your source files.

This way, you can forward declare your classes in the header, and define them in foo:

// foo.h
class bar;

class foo {
    bar * m_pbar;
}

// bar.h
class foo;
class bar {
    foo * parent;
}

That will allow you to work - you just can't put definitions that require member information into your header - move it to the .cpp file. The .cpp files can include both foo.h and bar.h:

// Foo.cpp
#include "foo.h"
#Include "bar.h"

void foo::some_method() {
     this->m_pbar->do_something(); // Legal, now, since both headers have been included
}
like image 182
Reed Copsey Avatar answered Sep 23 '22 00:09

Reed Copsey


In your example, foo depends on bar (because it contains an actual bar object), but bar does not depend on foo (since it only contains a foo *). Now bar does depend on on foo being a type name, so bar.h needs a forward declaration of the name: class foo;. foo depends on bar, so foo.h should contain #include "bar.h"

You can't have classes that directly depend on each other in C++; it simply doesn't work. You need to decouple the classes such that one only depends on the other existing, like in your example. The only things that create a direct dependence are using a class as a base class or as a field; any other use just creates an indirect dependence, which is generally not a problem.

like image 26
Chris Dodd Avatar answered Sep 19 '22 00:09

Chris Dodd