Re: polymorphic structs with "methods"



On Mar 23, 5:32 pm, Eric Sosman <esos...@xxxxxxxxxxxxxxxxxxxx> wrote:
Peter Michaux wrote:
/*
I want to have heterogeneous lists but treat all nodes the same
without checking some sort of struct "type" member and then using a
switch statement to call the appropriate function for that type. This
is in an effort to make my code more modular. Updating the switch
statement when a new "type" is added is not appealing. I've been
working on an idea (surely not original) about having polymorphic
struct "objects" that have function pointer members for "methods". My
code example is below and I would like to know where it sits in the
range from "hideous worst practice ever" to "yep people do that sort
of thing". If there are known improvements I could make I would
appreciate any comments, a link to a web page or book title.
*/

"Yep, people do that sort of thing." But in the rather
bare form you've presented, I think you'll find that you've
just traded one maintenance headache for another. You no
longer need to revisit all the switch statements when adding
a new type, but you *do* have to revisit all the existing
types when adding a new method. Also, as the repertoire of
methods grows, so too will the size of the Object struct and
the tedium of initializing each instance's pointers.

Some people have attacked both these problems by adding
one level of indirection. Instead of dragging all the function
pointers around in every instance, they carry just one pointer
to a single struct that contains all the method pointers for
the "class." For example,

[snip example code]

It is possible to use more elaborate schemes, too, which
may make it easier to manage "inheritance," dynamic creation of
new "classes," and so on.

However, I'd recommend against it. It's a kludge -- I don't
think there's any way to escape a lot of `void*' pointers and/or
a lot of casts, both of which are detrimental to type safety.
It's a kludge that was necessary a few decades ago, when C++
was a mish-mash of mutually incompatible semi-implementations
and Java hadn't been invented. But the toolchains are a lot
better than in the bad old days: C++ implementations are more
widespread and have converged toward their Standard, Java may
have been over-hyped at first but turns out to be quite useful,
and there are probably other languages that would meet most
reasonable sets of requirements.

Doing O-O in C is not putting lipstick on a pig -- C is not
a pig -- but it's a lot like teaching a pig to play the violin:
Just barely possible, perhaps, but not pleasing.

Very possible and very effective, see http://gnome.org.
But is often hard and unpleasant to deal with, certainly.

Yevgen
.



Relevant Pages

  • Re: polymorphic structs with "methods"
    ... without checking some sort of struct "type" member and then using a ... range from "hideous worst practice ever" to "yep people do that sort ... the tedium of initializing each instance's pointers. ... struct String { ...
    (comp.lang.c)
  • Re: polymorphic structs with "methods"
    ... without checking some sort of struct "type" member and then using a ... switch statement to call the appropriate function for that type. ... range from "hideous worst practice ever" to "yep people do that sort ...
    (comp.lang.c)
  • Re: Simple question, err... I think
    ... Your nodes contain no other indication of which pointers are valid, ... struct CountedObject ... int is_red; ... bool lament(char const s) ...
    (comp.programming)
  • Re: porting problems encountered
    ... Tru64 compiles long variables to size 8 bytes while VMS and HP-UX ... platform, the size of the structure would be 12 bytes. ... when it got to the AS/400 with 128 bit pointers. ... Using the struct from before: ...
    (comp.os.vms)
  • Re: fastest sorted list type?
    ... I maybe able to trim down the number but id still like to know wich is the ... I just have to use a dummy struct is all. ... method is declared and then passed to the Sort() method. ... bigger lists ... ...
    (microsoft.public.dotnet.languages.csharp)