The usual method is to pass a pointer or reference.
Example:
class Class2;
class Class1
{
void setFromClass2(const Class2& class2);
};
#include "class1.h"
#include "class2.h"
void Class1::setFromClass2(const Class2& class2)
{
}
You may also create a member variable in
Class1
that can be initialised with an instance of
Class2
. But then you must ensure that the
Class2
object exists during the life time of
Class1
or is reset and the code checks it:
class Class2;
class Class1
{
Class1() { m_class2 = NULL; }
void setClass2(const Class2 *class2) { m_class2 = class2; }
void someFunc();
protected:
const Class2 *m_class2;
};
void Class1::someFunc()
{
if (m_class2)
{
}
}
The
const
keyword in my examples ensures that the content of
Class2
can not be changed (read only).