linkage between classes

I have a class that I wrote, which is working nicely; I'll call it PacketStorer. Now I want to convert other existing code into another class which utilizes this one; let's call it PacketEater. Sub-classing PacketStorer doesn't seem appropriate here, since the two classes do different jobs.

So, the way I gave PacketEater access to PacketStorer, is I included a pointer to a PacketStorer in PacketEater, and passed an existing PacketStorer pointer into PacketEater's constructor. Well, it *does* work, but it's a very C way of doing things, I suspect it's not appropriate in C++... I know there's a feature called Friend in C++, which allows one class to know about members of another class, is that how I should establish connections between classes in this situation? Is there some other way to handle this? Or should I just stick with my C-style method of passing the class pointer into PacketEater??
You are confusing 'class' with 'object'
1
2
PacketStorer a,b,c;
PacketEater x;
¿which 'Storer' should 'x' eat?

An object should talk with:
_ Itself
_ Its members
_ Objects that it creates
_ Parameters to the method (part of the message)

According to which relationship an 'eater' and a 'storer' have, chose a communication medium.

friend allows to touch the private members. You still need the object to access.
Try to think of a class as a type. An object is an instance of said class.
Here's a bit of code to help elluminate the ambiguity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
class account_t
{
    std::string name;
    double balance;
    int acc_number;
};

int main()
{
   account_t dummy; //dummy is an instance account_t type
   account_t dummy2;
   dummy.name = "Joe";
   dummy2.name = "Bob";
}
Friends are great in life but not so great in C++. There are rare occasions when they can be used effectively but in most cases they just promote lazy design.

Your naming conventions are throwing me off but it sounds to me like you might want to use inheritance here.

Are there any similarities between PacketEater and PacketStorer? Do they use similar data in any way?

What functions or data in PacketStorer does PacketEater need to use? Give us a simple layout of the 2 classes.

No code, just the data types and method names.
Okay, let me gives some specifics of my two classes, to make the discussion more discrete (I avoided that at first because I was worried my post would be too long and people wouldn't read it! 8-{O ).

I have a class that implements a packet queue for handling received Ethernet packets. It has, of course, put_packet() and get_packet() functions. It uses a Windows Mutex to avoid pointer-access conflicts. My main purpose in creating the class was to conceal the mutex usage from the list user. (please disregard that I called the put() and get() functions push() and pop(), that was an error on my part, this is a queue, not a stack).

Now, I want to wrap another complicated piece of code inside a class, to simplify its usage. This function enables a Windows Event, then waits for a period of time for the Ethernet receive function to signal that a packet has been received and placed in the RX queue.

Here are the core elements of the two classes:

//************************************************
// packet queue class:
class CEthPacketList {
private:
... packet-list pointers, mutex and other internal data ...

public:
CEthPacketList(uint mutex_timeout, bool queueing_enabled) ;
~CEthPacketList() ;
void push_eth_packet(u8 *rx_bfr, uint bfr_len, u32 sender_ip);
packet_list_p pop_eth_packet(void);
} ;

//************************************************
// class to wait for packet-received event.
// (note that this class isn't written yet, this is hypothetical layout)

class CRxEventHandler {
private:
CEthPacketList *EthRxPacketQueue ; // pointer to instance of packet class

public:
CRxEventHandler(CEthPacketList *myPacketHandler) {
EthRxPacketQueue = myPacketHandler ;
} ;
int RxWaitFunc(u8 *rxbfr) {
// initialize the Event,
// wait for Event:
// if timeout transpires, return negative error code
// if Event is signaled, call
// EthRxPacketQueue->pop_eth_packet(),
// copy data into rxbfr, return packet byte count
} ;
} ;

So is this a thematic way to have the two classes interact, or is there some better way.

(BTW, it probably *is* reasonable for me to have CRxEventHandler be subclassed from CEthPacketList, but for this discussion what I'm wanting to think about is whether it is appropriate, generally, for one class to use a pointer to another to establish connections.)
Yes, one of the ways of representing a relationship is with a pointer to the other object.
It's quite flexible, as you can change the pointer or make it NULL.
Okay, thanks!! I just wanted to make sure there wasn't some other technique that was supposed to be used for this in C++ . This method also has the advantage of being familiar to someone who is a veteran C programmer!
Topic archived. No new replies allowed.