Re: Inheritance question
Andrea Crotti <andrea.crotti.0@gmail.com> wrote:
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 fact that you have already done this once in C will make things
easier. The simplest solution in this case would be to use the C code
you already wrote. If it works, you might as well.
Depending on what it looks like, the next step toward making it better
code would be to modularize it. Turn the structs into classes and move
the functions that are designed to maintain the class invariant into the
class. The next step after that would be to find opportunities for code
reuse/reduction. Do you have a lot of switch/case statements that all
use the same identifier? Then inheritance is called for. Do you have
several classes that all do the exact same thing with the same code and
the same data members? That might call for inheritance too, but it might
not.
(I'm adding comp.object because this is not strictly a C++ question.)