Re: Inheritance question
Victor Bazarov <v.bazarov@comcast.invalid> writes:
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).
Yes sure it's a Is-a, I just wrote also has-a in because it would also
be a possibility, but of course it doens't make much sense.
I mean I did a similar thing in C some time ago and there I used only
structures so it's a bit different, there were no is-a relation possible...
The advantage is that you don't have to maintain your own dynamic
buffer.
Yes that's not bad, but actually I should always know how much I'm
putting in memory, so it should not be a problem.
But if it makes my life easier why not, could be better.
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.
I meant more when you create a class
class X {
int x;
};
sizeof(new X()) == sizeof(int)
and is it guaranteed (more important)?
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).
Yes well but I don't have a struct if I create the class, or should I
maybe keep both approaches.
What I thought was something like
class X {
int x;
int y;
char buffer[100];
};
and doing
X b;
b.pack(char *result);
it returns in that buffer everything packed, where the fields with
size > 1 byte are converted in network order and everything is packed
(with __packed__ attribute).
Would that be possible/does it make any sense?
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
It's not so simple, what I'm doing is writing code for some network
simulation on top of a framework that is on top of omnet++ and some
libraries to compile on real machines.
I have enough stuff to deal with already, and it's not really much the
part of abstraction I need, but I want to do it as well as possible of
course since also other people might use it...