I'm hoping someone can please help me remember the C++ rule: When
you're writing a header file for a class (say, some_namespace::Bar),
and that class makes use of another class (some_namespace::Foo),
-------------------------------- snip --------------------------------
#ifndef GUARD_Foo_HPP
#define GUARD_Foo_HPP
namespace some_namespace {
class Foo
{
// Stuff.
};
} // namespace some_namespace.
#endif // GUARD_Bar_HPP
-------------------------------- snip --------------------------------
you have the option of either #including Foo.hpp,
-------------------------------- snip --------------------------------
#ifndef GUARD_Bar_HPP
#define GUARD_Bar_HPP
#include Foo.hpp
namespace some_namespace {
class Bar
{
// Stuff -- some of which uses Foo's.
};
} // namespace some_namespace.
#endif // GUARD_Bar_HPP
-------------------------------- snip --------------------------------
or else just making a forward declaration:
-------------------------------- snip --------------------------------
#ifndef GUARD_Bar_HPP
#define GUARD_Bar_HPP
namespace some_namespace {
// NOTE: We're *inside* the namespace declaration for this:
class Foo;
class Bar
{
// Stuff -- some of which uses Foo's.
};
} // namespace some_namespace.
#endif // GUARD_Bar_HPP
-------------------------------- snip --------------------------------
I realize that using the forward declaration is always preferable
(so as to make fewer dependencies, as well as less work for the
preprocessor and compiler), but what's the rule for when I can get
away with it?
Thanks.
--
--- remove zees if contacting via email ---