Re: Data abstraction in C
- From: "Alexei A. Frounze" <alexfru@xxxxxxx>
- Date: Wed, 31 Aug 2005 23:59:42 +0400
"jacob navia" <jacob@xxxxxxxxxxxxxxxx> wrote in message
news:431601bc$0$27440$8fcfb975@xxxxxxxxxxxxxxxxxx
....
> AND NOW (at last) you use it like this:
>
> #include "list.h"
> int main(void)
> {
> LIST *myList = newList(12);
>
> myList->lpVtbl->Append(myList,"Element1");
> myList->lpVtbl->Append(myList,"Element2");
> int d = myList->lpVtbl->Length(myList); // Should be 2
> }
....
> C is more explicit than C++, and there is no compiler help.
> This is less of a problem than what it seems at first sight,
> and MUCH more powerful. You are in control. There is no
> black box of a compiler to keep you from doing what you want.
>
> Also, there are no rules other than the rules you decide to follow.
Quite right. classes/objects, inheritance, etc etc, and the whole OOP aren't
magic and C++ and OOP aided languages aren't the only to benefit from the
OOP ideas and OOP-like functionality. Things like that can be implemented in
C and, obviously, :) in ASM. It's just that langs like C++ were
intentionally made to support OOP natively and easily, freeing the
programmer from writing some low level code and making the compiler take
care of that instead. But indeed, if the programmer isn't knowledgeable
enough, certain implicit things may appear obscure and unclear.
....
> For an example of this kind of programming see the source code
> of the container library in lcc-win32.
Great. In fact, I've done a lot of similar coding, and it seems to be the
proper way of doing things. Often times, the code needs not only be correct
but also needs to exhibit certain properties, such as multiple
instanciation, reentrancy, etc. Keeping the state (and everything that may
change) in the dedicated structure makes things easier. The only
global/static info hence should be:
- code itself
- constants
- a few variables that are initialized before the module is used and from
there on remain unchanged until the use is finished and some cleanup is due
Using pointers for the code, not just the data, (I mean the function
pointers) can make the code more flexible and general. This even helped me
to bugfix/patch some ROMed code at work. If the code was using direct calls,
the only option to change the behavior would be to drop the old code and use
the new. But with the function pointers, the cost of the fix was lowered. It
saved lots of memory (something you don't normally think of on a PC :) and
the embedded device cost.
Alex
.
- References:
- Data abstraction in C
- From: Anand Vardhan
- Re: Data abstraction in C
- From: jacob navia
- Data abstraction in C
- Prev by Date: Re: Style isn't always religious
- Next by Date: Re: how to write these info to file?
- Previous by thread: Re: Data abstraction in C
- Next by thread: Re: Data abstraction in C
- Index(es):
Relevant Pages
|