By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

class instances ..

P: n/a

Consider the case where I'm dealing with a vendor and a customer. The
customer initiates a request to the vendor (really a multithreaded
application hence semaphores are involved - in this case the customer
gives a semaphore to vendor). The vendor upon receipt of the semaphore
will do some 'processing'. When complete will do the same. ie. give a
semaphore to the customer. So now:

class vendor;
class customer {
vendor* ven;
public:
explicit customer ( vendor& ven_) : ven(ven_)
};

class vendor {
customer *ptr_cust;
public:
explicit vendor() : ptr_cust(0)
{ ptr_cust = new customer (*this); } //<- scary object not fully
constructed
~vendor() throw() { delete ptr_cust;
ptr_cust = 0; //<- just in case }
};

For cases where both classes need to access member functions within
each other. My thougths - in this case surrounds a higher level class
- call it 'notify'. Both customer and vendor will register their
objects with notify. This is where I'm 'stuck'. Not sure how to
achive this. Source example would be helpful

Thanks in advance.

Dec 9 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ma740988 wrote:
Consider the case where I'm dealing with a vendor and a customer. The
customer initiates a request to the vendor (really a multithreaded
application hence semaphores are involved - in this case the customer
gives a semaphore to vendor). The vendor upon receipt of the semaphore
will do some 'processing'. When complete will do the same. ie. give a
semaphore to the customer. So now:

class vendor;
class customer {
vendor* ven;
public:
explicit customer ( vendor& ven_) : ven(ven_)
};

class vendor {
customer *ptr_cust;
public:
explicit vendor() : ptr_cust(0)
{ ptr_cust = new customer (*this); } //<- scary object not fully
constructed
~vendor() throw() { delete ptr_cust;
ptr_cust = 0; //<- just in case }
};

For cases where both classes need to access member functions within
each other. My thougths - in this case surrounds a higher level class
- call it 'notify'. Both customer and vendor will register their
objects with notify. This is where I'm 'stuck'. Not sure how to
achive this. Source example would be helpful

Thanks in advance.


See the Mediator pattern in _Design Patterns_ by Gamma et al. It has
sample code.

Cheers! --M

Dec 9 '05 #2

P: n/a
See the Mediator pattern in _Design Patterns_ by Gamma et al. It has
sample code.

Cheers! --M


Of all the texts I possess, I don't think I have that one but will
check. First I'll start with the web.

The snippet below reflects an approach provided by a John Carson. A
nicety but I trying to peruse the alternatives.
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();

}

Dec 9 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.