Re: Cyclic Creation Dependency ?
Hi all,
First of all I would like to thank once more everyone for taking the
time to read this message and to reply to it.
The problem with the original code posted here is in the area of the
copy constructor for the COuterClass in that we didn't include the full
intended initialisation list.
When the compiler was attempting to build the code for the copy
constructor of the COuterClass - althought not called - it was
obviously trying to use the default constructor of the CInnerClass due
to the CInnerClass theInnerClass property. By changing the copy
constructor to:
COuterClass( const COuterClass& rhs ) :
theInnerClass( this )
{
cout << "COuterClass( const COuterClass& rhs )" << endl;
};
We explicitly forcing the compiler to use the explicit provide
constructor of the CInnerClass.
Here is the modified code, that works as originally intenteded.
class COuterClass;
//=============================================================================
//
// CInnerClass
//
//=============================================================================
class CInnerClass
{
public:
explicit CInnerClass( COuterClass* pOuterClass ) :
theOuterClass( pOuterClass )
{
cout << "CInnerClass( COuterClass* pOuterClass )" << endl;
}
private:
COuterClass* theOuterClass;
CInnerClass( ){};
CInnerClass( const CInnerClass& rhs){};
const CInnerClass& operator=( const CInnerClass& rhs ) {};
};
//=============================================================================
//
// COuterClass
//
//=============================================================================
class COuterClass
{
public:
COuterClass( ):
theInnerClass( this )
{
cout << "COuterClass( )" << endl;
};
CInnerClass theInnerClass;
private:
COuterClass( const COuterClass& rhs ) :
theInnerClass( this )
{
cout << "COuterClass( const COuterClass& rhs )" << endl;
};
const COuterClass& operator=( const COuterClass& rhs ) {};
};