472,975 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,975 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 2829

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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.