473,804 Members | 3,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cyclic Creation Dependency ?

Hi all,

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.

class COuterClass;

//=============== =============== =============== =============== =============== ==
//
// CInnerClass
//
//=============== =============== =============== =============== =============== ==
class CInnerClass
{
public:
COuterClass* theOuterClass;

explicit CInnerClass( COuterClass* pOuterClass ) :
theOuterClass( pOuterClass )
{
cout << "CInnerClas s( COuterClass* pOuterClass )" << endl;
}
private:
CInnerClass( ){};
CInnerClass( const CInnerClass& rhs){};
const CInnerClass& operator=( const CInnerClass& rhs ) {};
};

//=============== =============== =============== =============== =============== ==
//
// COuterClass
//
//=============== =============== =============== =============== =============== ==
class COuterClass
{
public:
CInnerClass theInnerClass;

COuterClass( ) :
theInnerClass( this )
{
cout << "COuterClas s( )" << endl;
};

private:

COuterClass( const COuterClass& rhs ){};
const COuterClass& operator=( const COuterClass& rhs ) {};
};
int main( ... )
{
COuterClass theOuterClass;
}

the error I am getting is :

--------------------Configuration: CyclicDependenc y - Win32
Debug--------------------
Compiling...
CyclicDependenc y.cpp
C:\Temp\CyclicD ependency\Cycli cDependency.cpp (54) : warning C4355:
'this' : used in base member initializer list
C:\Temp\CyclicD ependency\Cycli cDependency.cpp (61) : error C2248:
'CInnerClass::C InnerClass' : cannot access private member declared in
class 'CInnerClass'
C:\Temp\CyclicD ependency\Cycli cDependency.cpp (38) : see
declaration of 'CInnerClass::C InnerClass'
Error executing cl.exe.

CyclicDependenc y.exe - 1 error(s), 1 warning(s)

May 16 '06 #1
4 1737
* sa*********@bti nternet.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 << "CInnerClas s( 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 << "COuterClas s( )" << 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: CyclicDependenc y - Win32
Debug--------------------
Compiling...
CyclicDependenc y.cpp
C:\Temp\CyclicD ependency\Cycli cDependency.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.program mer.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?
May 16 '06 #2
<sa*********@bt internet.com> wrote in message
news:11******** **************@ y43g2000cwc.goo glegroups.com.. .
: Hi all,
:
: 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.
:
: class COuterClass;
:
:
//=============== =============== =============== =============== =============== ==
: //
: // CInnerClass
: //
:
//=============== =============== =============== =============== =============== ==
: class CInnerClass
: {
: public:
: COuterClass* theOuterClass;
:
: explicit CInnerClass( COuterClass* pOuterClass ) :
: theOuterClass( pOuterClass )
: {
: cout << "CInnerClas s( COuterClass* pOuterClass )" << endl;
: }
: private:
: CInnerClass( ){};
: CInnerClass( const CInnerClass& rhs){};
: const CInnerClass& operator=( const CInnerClass& rhs ) {};
: };
:
:
//=============== =============== =============== =============== =============== ==
: //
: // COuterClass
: //
:
//=============== =============== =============== =============== =============== ==
: class COuterClass
: {
: public:
: CInnerClass theInnerClass;
:
: COuterClass( ) :
: theInnerClass( this )
: {
: cout << "COuterClas s( )" << endl;
: };
:
: private:
:

The error is reported for this line: [ you should have indicated
this in your post ]
: COuterClass( const COuterClass& rhs ){};
The copy constuctor does not specify how to construct/initialize
member theInnerClass. Therefore its default constructor has
to be called -- but it is inaccessible (private to theInnerClass).

Has nothing to do with a cyclic dependency...

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
May 16 '06 #3
Cs**********@bt internet.com wrote:
Hi all,

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.

You're not calling the COuterClass copy constructor, you're assigning
the *POINTER* to an outerclass that was passed in as a parameter to the
constrcutor to the "theOuterCl ass" member.
class COuterClass;

//=============== =============== =============== =============== =============== ==
//
// CInnerClass
//
//=============== =============== =============== =============== =============== ==
class CInnerClass
{
public:
COuterClass* theOuterClass;

explicit CInnerClass( COuterClass* pOuterClass ) :
theOuterClass( pOuterClass )
{
cout << "CInnerClas s( COuterClass* pOuterClass )" << endl;
}
private:
CInnerClass( ){};
CInnerClass( const CInnerClass& rhs){};
const CInnerClass& operator=( const CInnerClass& rhs ) {};
};

//=============== =============== =============== =============== =============== ==
//
// COuterClass
//
//=============== =============== =============== =============== =============== ==
class COuterClass
{
public:
CInnerClass theInnerClass;
This declaration should blow up. CInnerClass has a private constructor
and COuterClass is not a friend.
COuterClass( ) :
theInnerClass( this )
{
cout << "COuterClas s( )" << endl;
};

private:

COuterClass( const COuterClass& rhs ){};
const COuterClass& operator=( const COuterClass& rhs ) {};
};
int main( ... )
{
COuterClass theOuterClass;
}

the error I am getting is :

--------------------Configuration: CyclicDependenc y - Win32
Debug--------------------
Compiling...
CyclicDependenc y.cpp
C:\Temp\CyclicD ependency\Cycli cDependency.cpp (54) : warning C4355:
'this' : used in base member initializer list
C:\Temp\CyclicD ependency\Cycli cDependency.cpp (61) : error C2248:
'CInnerClass::C InnerClass' : cannot access private member declared in
class 'CInnerClass'
C:\Temp\CyclicD ependency\Cycli cDependency.cpp (38) : see
declaration of 'CInnerClass::C InnerClass'
Error executing cl.exe.

CyclicDependenc y.exe - 1 error(s), 1 warning(s)


The warning about "this" on line 54 is safely ignorable. It's letting
you know that your object may not be fully constructed, so whatever
you're passing "this" to needs to be careful.

May 16 '06 #4
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 << "COuterClas s( 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 << "CInnerClas s( 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 << "COuterClas s( )" << endl;
};

CInnerClass theInnerClass;

private:

COuterClass( const COuterClass& rhs ) :
theInnerClass( this )
{
cout << "COuterClas s( const COuterClass& rhs )" << endl;
};

const COuterClass& operator=( const COuterClass& rhs ) {};
};

May 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2784
by: Brian Sabolik | last post by:
I'm not sure if I've broken any Object Oriented rules or not, but ... I have projects in 2 different solutions that need to use each other's methods. Therefore I may have an "update" method in project 1-solution A accessing a "save" method in project 2-solution B as well as "getinfo" method in project 2 accessing a "read" method in project A. Is this permitted? I am getting the "dependency file in project cannot be copied... conflict...
3
2240
by: Dennis Lerche | last post by:
Hi I have a problem regarding cyclic dependency, yeahh I know bad design. But right at this moment I can't see how it should be redesigned to avoid this. The problem is that I just can't get it to compile ..... I have two classes each having their own header files, including each other. A forward decleration doesn't seem to be enough because they also call function calls within the classes. How do I solve this ???
4
4553
by: Alex Sedow | last post by:
For example, System.dll contains assembly reference to System.XML.dll, System.XML.dll refers to System.dll. Some another open projects contain cyclic assembly dependencies too. How C# compiler may do that? Alex.
3
2124
by: Pohihihi | last post by:
This is a report from VSS newsgroup in hope of getting some more suggestions. Thanks, Po ------------------------------------------- I guess I am not getting the whole picture here. How does this apply to the condition when there is a cyclic reference for the binaries (dlls) in dotNet !! What I am thinking is that to use the real ProjectB object I will still need
3
2750
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex inclusion relationships. Another question I have is how to solve this dependency when "typedef" is used. usually we can use forward declaration like "struc A; " to solve references like "struct A* p". But in many places we have
1
677
by: pallav | last post by:
I have to header files, circuit.h and latch.h that reference each other and are causing a cyclic dependency. latch.h file #include "circuit.h" typedef boost::shared_ptr<struct LatchLatchPtr; class Latch {
3
1774
by: soup007 | last post by:
Hi, I am having some difficulties with cyclic dependency between two classes. Situation is something like following - ///A.h #include "B.h" class A { { int X; public:
11
3231
by: sgurukrupagmailcom | last post by:
Hi, When searching for a solution to my problem I stumbled upon 'Curiously Recurring Template Pattern' see link. This is how the pattern looks: template < typename T > struct y { } ;
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2983
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.