Re: Inheritance question
On 10/26/2010 12:58 PM, Andrea Crotti wrote:
Victor Bazarov<v.bazarov@comcast.invalid> writes:
First I rephrase better the "problem":
I need to manipulate network data, and I was wondering what could be the
best design to create a general packet and its subclasses.
For example a beacon is a network packet with some specific
informations.
So should it BE a Packet where the "content" contains the additional
informations, or should it HAVE a PacketHeader?
Perhaps I don't understand something... You state yourself that "a
beacon *is a* network packet". "Is-a" relationship is *usually*
modelled in C++ with the use of a public inheritance. It's so
commonplace that I am suspecting some kind of a trick here.
The "have" implementation either implies containment or private
inheritance. Conversion from 'beacon' to 'packet' is then either
prohibited or provided by you (by means of a type conversion function,
for example).
Maybe the first one is better since many functions could be reused...
What's the size? Why don't you simply use 'vector<char>'?
Yes maybe vector is better then, that thing should just be a pointer.
Since internally for many functions I have to use a
char *buffer
You can always extract the pointer to char from a vector, just take the
address of the first element (if it's not empty).
I don't know if I have advantages using a vector here.
The advantage is that you don't have to maintain your own dynamic buffer.
The size is also given to the constructor.
This would be Packet
--8<---------------cut here---------------start------------->8---
class Packet
{
private:
char type;
const char *buffer;
size_t size;
protected:
ostream& out;
public:
Packet(ostream& out);
Packet(ostream& out, char type, const char *buffer, int size);
virtual void pack(char *buffer);
friend ostream& operator<<(ostream& s, const Packet& c);
};
--8<---------------cut here---------------end--------------->8---
1. what does the "sizeof" return on a class exactly?
I meant, is it like doing the "sizeof" of a struct with the same fields
inside?
Uh... Yes.
Or something more?
In what sense can it be "more"? The size of the struct is the sum of
the sizes of its members, plus some optional padding.
And by the way "sizeof(ostream&)" returns a huge value, so I guess it's
not the size of the reference but of the object itself (which makes
sense of course).
Yes, since references aren't objects (and don't occupy storage) when you
use one as the argument of 'sizeof', you in fact are getting the size of
the object the reference refers to.
3. how would I pack the data?
something like?
void Packet::pack(const char *buffer) {
memcpy($first_field, ...)
What's the third argument to memcpy? Not sure what '$firs_field'
is. Weird syntax if you ask me.
I meant the classical way in C to create big buffers, like (very stupid
and wrong example...):
--8<---------------cut here---------------start------------->8---
int x = 10; int y = 20;
char buff[10];
memcpy(buff,&x, sizeof(int));
memcpy(buff + sizeof(int),&y, sizeof(int));
--8<---------------cut here---------------end--------------->8---
I guess in the end I stil have to do something like this, but I can
encapsulate it better.
If you put your 'ints' in a struct, you can simply memcpy the entire
struct (instead of doing the packing yourself).
"Sanity checks"?
Well checking if the "packet" fits in the buffer I give as input to the
function for example.
Given this beacon
--8<---------------cut here---------------start------------->8---
class Beacon : public Packet
{
private:
wf_simtime_t timestamp; // timestamp of creation
serial_t nr;
serial_t snr;
// can be done also since it has a 0-arguments constructor
Node&sender;
public:
Beacon(ostream& out);
// Beacon(const Packet&);
// virtual ~Beacon();
};
--8<---------------cut here---------------end--------------->8---
I don't find what should I have in the Beacon constructor to make also
the Packet constructor "happy"...
Now that you've explained it a bit better, I am thinking that there are
*numerous* libraries for network communication. Why do you think you
have to reinvent the wheel?
V
--
I do not respond to top-posted replies, please don't ask