OOP in C!

  • Thread starter Prashanth Ellina
  • Start date
P

Prashanth Ellina

Hi,

I have a feeling that OOP can be done in C also. I have used a
structure to hold member variables and function pointers. The
structure is used as a class to create new 'objects'. But I hit a
problem. How do I access these 'member variables' from the function
that is pointed to by the function pointer in the structure.

I would really appreciate help with this and a code sample will be
wonderful.

Thanks in advance,
Prashanth Ellina
 
M

Mike Wahler

Prashanth Ellina said:
Hi,

I have a feeling that OOP can be done in C also.

It can.
I have used a
structure to hold member variables and function pointers. The
structure is used as a class to create new 'objects'. But I hit a
problem. How do I access these 'member variables' from the function
that is pointed to by the function pointer in the structure.

There's no 'automatic' way. You'll need to pass the
structure (or usually best, a pointer to it) to your
function.
I would really appreciate help with this and a code sample will be
wonderful.

#include <stdio.h>

struct S
{
int member;
void (*fp)(struct S *);
};

void func(struct S *param)
{
printf("%d\n", param->member);
}

int main()
{
struct S object = {42, func};
func(&object); /* prints 42 */
return 0;
}

-Mike
 
E

E. Robert Tisdale

Prashanth said:
I have a feeling that [Obect Oriented Programming] can be done in C also.
Correct.

I have used a structure to hold member variables and function pointers.

Pointers to *callback* functions.
The structure is used as a class to create new 'objects'.
But I hit a problem.
How do I access these 'member variables' from the function
that is pointed to by the function pointer in the structure.

You must pass a pointer to the function explicitly.
I would really appreciate help with this
and a code sample will be wonderful.

typedef struct X {
int i;
const
struct X* (*f)(const struct X*);
} X;

const
X* X_actual_f(const X* this) {
fprinf(stdout, "this->i = %d\n", this->i);
return this;
}

inline static
X X_create(int i) {
X value;
value.i = i;
value.f = X_actual_f;
return value;
}

But actual implementations of run-time polymorphism
don't store pointers to functions in the object itself.
They store a pointer to a virtual function table
which is initialized in the class definition so that
the virtual functions are associated with the *class*
and *not* any object which is an instance of that class.

typedef struct X {
int i;
const
void* pV; // virtual function table pointer
} X;

const
X* X_actual_f(const X* this) {
fprinf(stdout, "this->i = %d\n", this->i);
return this;
}

struct vTable_t {
X* (*f)(const X*);
} vTable_t;

vTable_t X_vTable = { X_actual_f; };

X X_create(int i) {
X value;
value.i = i;
value.pV = (void*)(&X_vTable);
return value;
}

const
X* X_virtual_f(const X* this) {
return ((vTable_t*)(this->pV))->f(this);
}

You call X_virtual_f(const X*) instead of X_actual_f(const X*)
so that it will work for classes

typedef struct Y {
X x;
// . . .
} Y;

const
X* Y_actual_f(const Y*);

derived from X
but call Y_actual_f(const Y*) instead of X_actual_f(const X*).

Go to Google Groups:

http://groups.google.com/

and search for

run-time polymorphism in C (long)

in the comp.lang.c newsgroup. You should find:

From: E. Robert Tisdale ([email protected])
Subject: run-time polymorphism in C (long)
Newsgroups: comp.lang.c
Date: 2004-05-12 22:55:28 PST
 
C

Christian Bau

"E. Robert Tisdale said:
Prashanth said:
I have a feeling that [Obect Oriented Programming] can be done in C also.
Correct.

I have used a structure to hold member variables and function pointers.

Pointers to *callback* functions.

You are confused. Callback functions are just a special case of function
pointers with a specific use. Virtual functions in object oriented
languages or function pointers used in C to emulate virtual functions
are usually not callback functions.
 
E

E. Robert Tisdale

Christian said:
E. Robert Tisdale"wrote:
Prashanth said:
I have a feeling that [Obect Oriented Programming] can be done in C also.
Correct.

I have used a structure to hold member variables and function pointers.

Pointers to *callback* functions.


Callback functions are just a special case of function pointers
with a specific use. Virtual functions in object oriented languages
or function pointers used in C to emulate virtual functions
are usually not callback functions.

Please cite a passage in then C 99 standard
(or even in your favorite glossary)
that defines callback functions this way.
 
C

Christian Bau

"E. Robert Tisdale said:
Christian said:
E. Robert Tisdale"wrote:
Prashanth Ellina wrote:

I have a feeling that [Obect Oriented Programming] can be done in C also.

Correct.

I have used a structure to hold member variables and function pointers.

Pointers to *callback* functions.


Callback functions are just a special case of function pointers
with a specific use. Virtual functions in object oriented languages
or function pointers used in C to emulate virtual functions
are usually not callback functions.

Please cite a passage in then C 99 standard
(or even in your favorite glossary)
that defines callback functions this way.

There is the troll again: "Please cite a passage..."

What about doing the legwork yourself and citing a book yourself.
 
W

Wavemaker

Christian Bau said:
E. Robert Tisdale said:
Prashanth said:
I have a feeling that [Obect Oriented Programming]
can be done in C also.
Correct.

I have used a structure to hold member variables and
function pointers.

Pointers to *callback* functions.

You are confused. Callback functions are just a special
case of function pointers with a specific use.

Could you clarify this? It sounds as though you are saying callback
functions are function pointers.
 
C

Christian Bau

"E. Robert Tisdale said:
Take a look at Chapter 10: Delegates Callback Functions.

Thanks. It shows exactly what I said: The term "callback functions" is
used for a certain use of function pointers, and not, as you believed,
for function pointers in general or for function pointers used to
emulate member functions in general.

See, you are wrong again.
 
C

Christian Bau

"Wavemaker said:
Christian Bau said:
E. Robert Tisdale said:
Prashanth Ellina wrote:

I have a feeling that [Obect Oriented Programming]
can be done in C also.

Correct.

I have used a structure to hold member variables and
function pointers.

Pointers to *callback* functions.

You are confused. Callback functions are just a special
case of function pointers with a specific use.

Could you clarify this? It sounds as though you are saying callback
functions are function pointers.

Yes. Callback functions are usually implemented through function
pointers, or in an object oriented language the could be implemented
through member functions. HOWEVER function pointers and member functions
are not generally used as callback functions.

It's like integers and prime numbers: Of course all prime numbers are
integers. But it would be Tisdale-like nonsense to claim that all
integers are prime numbers. Just like claiming that function pointers
are callback functions is nonsense.

A typical example of a callback function is the function pointer that
you pass to qsort. The important thing is that the design of this
function has been done to fulfill the needs of the qsort function; most
likely you would never have written that function if you didn't want to
call qsort. Even if you had written a function to compare two objects,
there are dozens of ways how you could have defined the prototype for
that function, and most likely it wouldn't have matched the prototype
required by qsort.

So it is actually unlikely that a member function would be used as a
callback function - unless the class in question has been specifically
designed to match the code requiring the callback function.
 
G

Gustavo Cipriano Mota Sousa

Christian Bau said:
Wavemaker said:
Christian Bau said:
:
Prashanth Ellina wrote:

I have a feeling that [Obect Oriented Programming]
can be done in C also.

Correct.

I have used a structure to hold member variables and
function pointers.

Pointers to *callback* functions.

You are confused. Callback functions are just a special
case of function pointers with a specific use.

Could you clarify this? It sounds as though you are saying callback
functions are function pointers.

Yes. Callback functions are usually implemented through function
pointers, or in an object oriented language the could be implemented
through member functions. HOWEVER function pointers and member functions
are not generally used as callback functions.

It's like integers and prime numbers: Of course all prime numbers are
integers. But it would be Tisdale-like nonsense to claim that all
integers are prime numbers. Just like claiming that function pointers
are callback functions is nonsense.

A typical example of a callback function is the function pointer that
you pass to qsort. The important thing is that the design of this
function has been done to fulfill the needs of the qsort function; most
likely you would never have written that function if you didn't want to
call qsort. Even if you had written a function to compare two objects,
there are dozens of ways how you could have defined the prototype for
that function, and most likely it wouldn't have matched the prototype
required by qsort.

So it is actually unlikely that a member function would be used as a
callback function - unless the class in question has been specifically
designed to match the code requiring the callback function.

Aren't callback functions just functions whose pointer is used as an
argument to a function?

int callback_func(int x) {
return (x + 1);
}

void exec_callback(int (*func_p)(int)) {
printf("%d\n", func_p(10));
}

void main() {
exec_callback(&callback_func);
}

Is callback_func in this example a callback functino?
 
E

E. Robert Tisdale

Gustavo said:
Aren't callback functions just functions
whose pointer is used as an argument to a function?

int callback_func(int x) {
return (x + 1);
}

void exec_callback(int (*func_p)(int)) {
printf("%d\n", func_p(10));
}

int main(int argc, char* argv[]) {
exec_callback(&callback_func);
}

Is callback_func in this example a callback function?

Yes.
And callback functions can also be *installed* (*registered*)
in a global variable or data structure to be called later.

int callback_func1(int x) {
return (x + 1);
}

int callback_func2(int x) {
return (x + 2);
}

typedef int (*callback_func_t)(int);
callback_func_t callback_func;

callback_func_t register_callback(callback_func_t f) {
callback_func_t t = callback_func;
callback_func = f;
return t;
}

void exec_callback(callback_func_t func_p) {
printf("%d\n", func_p(10));
}

int main(int argc, char* argv[]) {
register_callback(callback_func1);
exec_callback(callback_func);
return 0;
}

Signal handlers

#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);

are callback functions.

In the typical implementation of run-time polymorphism,
pointers to callback functions are installed in
the virtual function table (vtable) for the class
and each object of that type is initialized
with a reference (pointer) to the corresponding vtable.
 
M

Malcolm

Gustavo Cipriano Mota Sousa said:
Aren't callback functions just functions whose pointer is used as
an argument to a function?
Exactly. (An exception would be if the function pointer is not used to call
a function, but maybe has its bit pattern printed out or something like
that).

When you are using function pointers very heavily to implement some sort of
object-oriented design scheme it ceases to be very useful to call them
"callback functions", which implies a relatively limited function which
quickly returns control to the caller.

Also, occasionally one might want to use function pointers not as arguments
to functions. You might for example have an array of function pointers that
you index into. These wouldn't qualify as "calback functions".
 
E

E. Robert Tisdale

Malcolm said:
Exactly. (An exception would be
if the function pointer is not used to call a function,
but maybe has its bit pattern printed out or something like that).

When you are using function pointers very heavily to implement some sort of
object-oriented design scheme it ceases to be very useful to call them
"callback functions", which implies a relatively limited function
which quickly returns control to the caller.

Who told you that?
I don't recall any such restriction -- either explicit or implicit --
on callback functions.
Also, occasionally,
one might want to use function pointers not as arguments to functions.
You might for example have an array of function pointers that you index into.
These wouldn't qualify as "callback functions".

Why not?
 
C

Christian Bau

"E. Robert Tisdale said:
Who told you that?
I don't recall any such restriction -- either explicit or implicit --
on callback functions.

Why not?

"Callback function" refers to usage. "Function pointer" refers to
implementation. Function pointers that are not used as callback
functions are not callback functions.
 
E

E. Robert Tisdale

Christian said:
"Callback function" refers to usage.
"Function pointer" refers to implementation.
Function pointers that are not used as callback functions
are not callback functions.

Please show an example
where a function pointer is *not* used to implement a callback function.
 
E

E. Robert Tisdale

Stephen said:
jump tables

Yes, a virtual function table may be implemented as a jump table.
But the function pointers in a jump table point to callback functions.
 
C

Christian Bau

"E. Robert Tisdale said:
Yes, a virtual function table may be implemented as a jump table.
But the function pointers in a jump table point to callback functions.

Since your little brain doesn't have enough braincells to understand the
difference between a function pointer and a callback function, giving an
example to you would be pointless.
 

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

Similar Threads

Pointer casts for OOP 2
C exercise 1
Meme generator in c 1
Copy string from 2D array to a 1D array in C 1
OOP 13
Filter sober in c++ don't pass test 0
[C#] Extend main interface on child level 0
Learning OOP 1

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top