Re: Adding the ability to add functions into structures?



Keith Thompson a écrit :

One difficulty with this approach, as compared to the equivalent in a language like C++ that support OO directly, is that you have to manually build and initialize the function table yourself; the compiler won't do it for you. If you forget to do this, Bad Things Happen; for example:

    struct ArrayList obj;
    int count = obj.lpVtbl->GetCount();

invokes undefined behavior.  But that's not a fatal flaw; it's easy
enough to declare an object that provides a default initial value:

    const struct ArrayListInterface ArrayListInterfaceDefault = {
        ...
    };

    struct ArrayList ArrayListDefault = {
        &ArrayListInterfaceDefault,
        0,
        ...
    }

and then:

    struct ArrayList obj = ArrayListDefault;
    int count = obj.lpVtbl->GetCount();

will consistently give you 0.

Normally you would call a constructor function that returns a pointer to an allocated structure.

The advantage of this is the flexibility it gives you. You can change
the behavior of a function at run time, by assigning the function
pointer to another function. That function can call the old function
pointer and then do something special, or can reimplement the
old function completely. And this is achieved without having to change
a single line in user code.

The array list object is patterned like its name-sake in C#: a flexible
array that can hold items and grows automatically if needed.

Another plus point is that the name-space of the interface is private
to the containing structure, so you can use simple names like "Add"
or "Delete" without fear that  it will clash with the user name space.

jacob
.



Relevant Pages

  • Re: Parsing a PATH null-terminated variable
    ... In case you miss the compiler warning though and use an uninitialized ... variable the program "may" work because the uninitialized pointer can ... On the other hand if you initialize the pointer to a ... writing through a null pointer will crash but reading will ...
    (comp.lang.c)
  • Re: Test for nil causes AV in D4
    ... pointer to examine in more details if the pointer is looking at a 0 ... The compiler isn't that smart. ... it lives in memory that is loaded from ... is related to the fact that you can't initialize variables when they are ...
    (comp.lang.pascal.delphi.misc)
  • Re: pointers?
    ... that doesn't point to an object, and not bother to initialize ... What a C compiler *should* do. ... I think you'll find precious few real-world C compilers that will make the specific optimization you're talking about here, making this discussion interesting from a theoretical or "language Nazi" standpoint, but completely uninteresting from a real-world, in-the-trenches, ... The only fundamental difference that I see between a pointer in C and an address in Forth is that the pointer knows the size of the object it points to. ...
    (comp.lang.forth)
  • Re: pointers?
    ... that doesn't point to an object, and not bother to initialize ... What a C compiler *must* do. ... and an address in Forth is that the pointer (or more accurately the ...
    (comp.lang.forth)
  • Re: How to convert Infix notation to postfix notation
    ... and make all strings const save where the intent ... function whose contract is to change the string. ... the compiler "just" prevents the string ... try to do using the pointer you get. ...
    (comp.lang.c)