A
Aguilar, James
Suppose I have classes Foo and Bar. They depend on each other, but other
classes also depend on them. Moreover, their functionality is distinct.
How can I include both of them?
Specifically, Foo is a class that holds pointers to a variety of objects, as
in:
class Foo
{
public: ...
private:
Bar a
Baz b
FooBar c
}
So, I include the headers for Bar, Baz, and FooBar like so:
//Foo.hh
#ifndef _FOO_HH_
#define _FOO_HH_ 1
#include "Bar.hh"
#include "Baz.hh"
#include "FooBar.hh"
.... //class defn.
#endif
But the class bar needs a pointer to the Foo object that calls its
constructor. So:
//Bar.hh
#ifndef _BAR_HH_
#define _BAR_HH_ 1
#include "Foo.hh"
.... //class defn.
#endif
However, if I'm compiling Bar.hh, it doesn't work, because when it Foo.hh
tries to include Bar.hh, it finds that _BAR_HH_ is defined. What is the
solution to this problem? I was thinking of a .hh files with declarations
of all the various classes that I will define at any point and putting that
at the top of every header file. Is that good?
classes also depend on them. Moreover, their functionality is distinct.
How can I include both of them?
Specifically, Foo is a class that holds pointers to a variety of objects, as
in:
class Foo
{
public: ...
private:
Bar a
Baz b
FooBar c
}
So, I include the headers for Bar, Baz, and FooBar like so:
//Foo.hh
#ifndef _FOO_HH_
#define _FOO_HH_ 1
#include "Bar.hh"
#include "Baz.hh"
#include "FooBar.hh"
.... //class defn.
#endif
But the class bar needs a pointer to the Foo object that calls its
constructor. So:
//Bar.hh
#ifndef _BAR_HH_
#define _BAR_HH_ 1
#include "Foo.hh"
.... //class defn.
#endif
However, if I'm compiling Bar.hh, it doesn't work, because when it Foo.hh
tries to include Bar.hh, it finds that _BAR_HH_ is defined. What is the
solution to this problem? I was thinking of a .hh files with declarations
of all the various classes that I will define at any point and putting that
at the top of every header file. Is that good?