473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

circular dependency...

Hi,

I have an issue which is the ordinary and always recurring circular
dependency - but I do not know how to resolve it :-( The usual forward
declaration does not help...

I have a client that uses a resource. As a consequence of using the
resource, the resource later receives an asynchronous notification (from an
underlying layer, e.g. OS). This notification should then be forwarded to
the Client that owns the resource.

In code this looks something:

int main()
{
Resource resource; // Define the resource
Client client(&resource); // Configure the client to own the resource
client.Execute(); // Execute operation, which in turn uses the
resource

RunEventLoopForever();
}
class Resource
{
Client* m_client;
public:
void SetClient(Client* client)
{
m_client = client;
}

void Use()
{
// Use this resource
// DoSomethingInteresting()
}
void AsyncNotification()
{
m_client->Notify();
}
};

class Client
{
Resource* m_resource;
public:
Client(Resource* res) : m_resource(res)
{
m_resource->SetClient(this);
}
void Execute()
{
m_resource->Use();
}
void Notify()
{
// Handle notification
// m_resource->Whatever()
}
};

What should I do to resolve this issue?
Complete redesign? Rather not - I like this simple design (besides it
doesn't work...)
Introducing something in between to break the dependency? If so - what?

Thanks in advamce!
/Rob
Nov 28 '05 #1
3 2855

Rob Clark wrote:
class Resource
{
Client* m_client;
public:
void SetClient(Client* client)
{
m_client = client;
}

void Use()
{
// Use this resource
// DoSomethingInteresting()
}
void AsyncNotification()
{
m_client->Notify();
}
};

class Client
{
Resource* m_resource;
public:
Client(Resource* res) : m_resource(res)
{
m_resource->SetClient(this);
}
void Execute()
{
m_resource->Use();
}
void Notify()
{
// Handle notification
// m_resource->Whatever()
}
};

What should I do to resolve this issue?


Donot define member functions inside the class. Simply declare them.
Define them outside the class. Use inline if you want them to be
inlined. Then simply declare the required class.

//header file Resource.h
class Client;
class Resource
{
Client* m_client;
public:
void SetClient(Client* client);
void Use();
void AsyncNotification();
};

// definitions will go in Resource.cpp. Include Resource.h there.

HTH.

Nov 28 '05 #2
"Rob Clark" <se@ms.com> wrote in message
news:v%***********@nntpserver.swip.net
Hi,

I have an issue which is the ordinary and always recurring circular
dependency - but I do not know how to resolve it :-( The usual
forward declaration does not help...

I have a client that uses a resource. As a consequence of using the
resource, the resource later receives an asynchronous notification
(from an underlying layer, e.g. OS). This notification should then be
forwarded to the Client that owns the resource.

In code this looks something:
Plainly the Resource and Client classes must be declared prior to main(),
quite apart from any circular dependency issues.

int main()
{
Resource resource; // Define the resource
Client client(&resource); // Configure the client to own the
resource client.Execute(); // Execute operation, which in turn
uses the resource

RunEventLoopForever();
}
class Resource
{
Client* m_client;
public:
void SetClient(Client* client)
{
m_client = client;
}

void Use()
{
// Use this resource
// DoSomethingInteresting()
}
void AsyncNotification()
{
m_client->Notify();
}
This is where your fundamental problem lies. You are calling a member
function of Client before the client class has been defined. The solution is
to only declare AsyncNotification here and define it later.
};

class Client
{
Resource* m_resource;
public:
Client(Resource* res) : m_resource(res)
{
m_resource->SetClient(this);
}
void Execute()
{
m_resource->Use();
}
void Notify()
{
// Handle notification
// m_resource->Whatever()
}
};

What should I do to resolve this issue?
Complete redesign? Rather not - I like this simple design (besides it
doesn't work...)
Introducing something in between to break the dependency? If so -
what?
Thanks in advamce!
/Rob


The following compiles:

class Client;

class Resource
{
Client* m_client;
public:
void SetClient(Client* client)
{
m_client = client;
}

void Use()
{
// Use this resource
// DoSomethingInteresting()
}
void AsyncNotification();
};

class Client
{
Resource* m_resource;
public:
Client(Resource* res) : m_resource(res)
{
m_resource->SetClient(this);
}
void Execute()
{
m_resource->Use();
}
void Notify()
{
// Handle notification
// m_resource->Whatever()
}
};

void Resource::AsyncNotification()
{
m_client->Notify();
}
int main()
{
Resource resource; // Define the resource
Client client(&resource); // Configure the client to own the resource
client.Execute(); // Execute operation, which in turn uses the
resource

//RunEventLoopForever();
}
--
John Carson

Nov 28 '05 #3
Oh so obvious when you have the answer - thank you both guys, Neelesh and
John!
/Rob
Nov 28 '05 #4

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

Similar topics

4
by: Evelyne | last post by:
On a server W2K, IIS5, I have many messages from ASP : cirdular dependency between types/modules. Which can be the origin of the problem? Example : Event Type: Warning Event Source: Active...
1
by: Henry Miller | last post by:
I have the following code (much simplified for this post). Note that SessionKey uses DataAccess, and DataAccess requires SessionKey in it's constructor. Public Class SessionKey Public...
2
by: ernesto basc?n pantoja | last post by:
Hi everybody: I'm implementing a general C++ framework and I have a basic question about circular dependencies: I am creating a base class Object, my Object class has a method defined as:...
4
by: ro86 | last post by:
Hello everyone! I am a newbie to C++ (~1 Week experience) and I have a few months of experience with object-oriented languages (Objective-C). I am currently working just for fun on a particle...
1
by: ChrisB | last post by:
Hello: I have created several components that have the following dependency relationships: Assembly1 references assemblies A, B, and C. Assembly2 references assemblies A, B When attempting...
4
by: Henke | last post by:
I have this scenario. public class A { public int numbers; public class A() { }
2
by: Rimma Meital via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Rimma Meital I have 2 DLLs (Class Libraries). Both defines single-ton objects - logger object (writes to logger) and...
7
by: barias | last post by:
Although circular dependencies are something developers should normally avoid, unfortunately they are very easy to create accidentally between classes in a VS project (i.e. circular compile-time...
3
by: donnyma | last post by:
I have a problem that looks like it has not been discussed before in these groups. I have a simple SQLAgent job that runs sp_who (could be anything, but let's just say sp_who for this example). ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.