Data abstraction in C

A

Anand Vardhan

Hi

Could anyone tell me if there is any way to do data abstraction in C
or use object oriented methods. I was thinking about using structures
but I am getting no where.

Regards.
 
E

E. Robert Tisdale

Anand said:
Could anyone tell me if there is any way to do data abstraction in C
or use object oriented methods?
I was thinking about using structures but I am getting no where.

Run-time polymorphism has been discussed at length
in the comp.lang.c newsgroup. See Google Groups

http://groups.google.com/

and search for

Tisdale Shape group:comp.lang.c.*
 
E

Emmanuel Delahaye

Anand Vardhan wrote on 31/08/05 :
Could anyone tell me if there is any way to do data abstraction in C
or use object oriented methods. I was thinking about using structures
but I am getting no where.

http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 
J

jacob navia

Anand said:
Hi

Could anyone tell me if there is any way to do data abstraction in C
or use object oriented methods. I was thinking about using structures
but I am getting no where.

Regards.
You can define interfaces like this:

// In a header file you do:
struct interface; // forward declaration

typedef struct tagListElement {
struct tagListElement *Next;
void *Data;
} ListElement;

typedef struct tagList {
struct interface * lpVtbl; // Pointer to the function table
// Data fields
ListElement *Last;
ListElement *First;
int NumberOfElements;
} LIST;

// Define the function table (methods)
struct interface {
int (*Append)(LIST *L,void *data);
void *(*First)(LIST *L);
void *(*Last)(LIST *L);
void *(*Remove)(LIST *L);
int (*Length)(LIST *L);
// Just an example. Many functions are missing
// But you get the idea.
};

// Now, in an implementation file you do:

static int Append(LIST *L,void *data)
{ // Implementation omitted }

static void *First(LIST *L)
{ // Implementation omitted }

static void *Last(LIST *L)
{ // Implementation omitted }

static void *Remove(LIST *L)
{ // Implementation omitted }

static int Length(LIST *L)
{ // Implementation omitted }

// Define a static struct interface holding
// function pointers to the implementation.
static struct interface {
First, Last,
Remove, Length
} MethodTable;

// Define the constructor.
LIST *newList(int NumberOfElements)
{
LIST *result;
result = malloc(sizeof(LIST));
if (result) {
memset(result,0,sizeof(LIST));
// assign the method table
result->lpVtbl = & MethodTable;
// assign the other fields
}
return result;
}

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
}

This is quite powerful since you can at any time replace
those function pointers by a function of your own (subclassing)
but you can keep a copy of the function pointer to call the
original method either before or after or in between your
own subclassed method, what you can't do in C++.

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.

Of course this can be used constructively or can lead to code
that is impossible to follow. It is up to you.

For an example of this kind of programming see the source code
of the container library in lcc-win32.

http://www.cs.virginia.edu/~lcc-win32.
 
A

Alexei A. Frounze

....
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
 
K

Keith Thompson

E. Robert Tisdale said:
Run-time polymorphism has been discussed at length
in the comp.lang.c newsgroup. See Google Groups

http://groups.google.com/

and search for

Tisdale Shape group:comp.lang.c.*

And be sure to read any responses to ERT's articles. His statements
are frequently refuted by the knowledgable members of the group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top