Форвардное объявление - друг:
// foo.h
class foo2;
class foo
{
foo2 *pFoo2;
};
// foo2.h
#include "foo.h"
class foo2
{
foo *pFoo;
};
Однако, как говорит Пабби, классы, которые должны знать друг о друге, вероятно, должны быть просто одним классом или, возможно, классом с двумя членами, оба изкоторые знают о родительском классе, но не как двусторонние отношения.
Что касается родительства и универсальности:
template <class Parent>
class ChildOf
{
public:
// types
typedef Parent ParentType;
// structors
explicit ChildOf(Parent& p);
~ChildOf();
// general use
Parent& GetParent();
const Parent& GetParent() const;
void SetParent(Parent& p);
private:
// data
Parent *m_pParent;
};
/*
implementation
*/
template <class ParentType>
ChildOf<ParentType>::ChildOf(ParentType& p)
: m_pParent(&p)
{}
template <class Parent>
ChildOf<Parent>::~ChildOf()
{}
template <class ParentType>
inline
ParentType& ChildOf<ParentType>::GetParent()
{
return *m_pParent;
}
template <class ParentType>
inline
const ParentType& ChildOf<ParentType>::GetParent() const
{
return *m_pParent;
}
template <class ParentType>
void ChildOf<ParentType>::SetParent(ParentType& p)
{
m_pParent = &p;
}