class instances ..

M

ma740988

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.
 
M

mlimber

ma740988 said:
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
 
M

ma740988

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();



}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top