Re: Cyclic Creation Dependency ?
* sakis.panou@btinternet.com:
Can anyone explain to me why the copy constructor of the COuterClass is
getting called for this one? Let me start by saying I reckon this is
seriously bad way of implementing anything of the sort, we have a way
around this. I am simply trying to understand the order or creation and
the reason for the call to the COuterClass copy constructor. Any help
would be seriously appreciated. Thanks in advance.
Here you need
#include <iostream>
#include <ostream>
using namespace std;
but instead of the 'using' directive, do consider using explicit
qualification like 'std::cout' -- it's a good habit.
class COuterClass;
//=============================================================================
//
// CInnerClass
//
//=============================================================================
class CInnerClass
{
public:
COuterClass* theOuterClass;
explicit CInnerClass( COuterClass* pOuterClass ) :
theOuterClass( pOuterClass )
{
cout << "CInnerClass( COuterClass* pOuterClass )" << endl;
}
private:
CInnerClass( ){};
CInnerClass( const CInnerClass& rhs){};
const CInnerClass& operator=( const CInnerClass& rhs ) {};
Remove the '{}' empty implementations.
};
//=============================================================================
//
// COuterClass
//
//=============================================================================
class COuterClass
{
public:
CInnerClass theInnerClass;
COuterClass( ) :
theInnerClass( this )
{
cout << "COuterClass( )" << endl;
};
private:
COuterClass( const COuterClass& rhs ){};
const COuterClass& operator=( const COuterClass& rhs ) {};
Remove the '{}' empty implementations.
The first one is the cause of your compilation error: it uses the
CInnerClass copy constructor.
};
int main( ... )
Your compiler, Microsoft's Visual C++, accepts this non-standard
signature 'main', as it's allowed to. But it's very bad form. Remove
the three dots.
{
COuterClass theOuterClass;
}
the error I am getting is :
--------------------Configuration: CyclicDependency - Win32
Debug--------------------
Compiling...
CyclicDependency.cpp
C:\Temp\CyclicDependency\CyclicDependency.cpp(54) : warning C4355:
'this' : used in base member initializer list
The documentation for you compiler, Microsoft's Visual C++, incorrectly
states that "The this pointer is valid only within nonstatic member
functions. It cannot be used in the initializer list for a base class.".
That's rubbish.
Turn off the silly-warning by using that compiler's #pragma mechanism
(it's a very good compiler when you configure it to be
standard-conforming, but it's one of the very worst out of the box).
I've set follow-ups to [comp.os.ms-windows.programmer.win32].
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?