OOP in C!

  • Thread starter Prashanth Ellina
  • Start date
R

Rufus V. Smith

Malcolm said:
OK here's an example.

/*
return the maximum value of an stdlib maths function that takes one scalar
as its argument.
*/
double maximum( double (*fptr)(double x) )
{
if( fptr == sin)
return 1.0;
if(fptr == sqrt)
return DBL_MAX;
if(fptr == asin)
return 3.14 * 2.0;

/* add extra for the other function in the maths library */
}

Good One Malcolm!

Almost made me wet my pants.

I've been absent from the newsgroups for a time (unemployment can do that),
but was following this thread with some uneasy interest.

At first I thought, "of course function pointers aren't used only for
callback functions." But after reading and thinking, I decided that
ultimately,
if that function pointer is every actually used it is effectively "calling
back" to
the function it points to.

However, your fine example shows that if we use the function pointers only
as pointers, there is no "calling", hence no "callback" happening.

Which brings me back to one observation, though:

function pointers are NEVER callback functions

They are POINTERS TO callback functions.

let me slip back into my teapot now.

Rufus
 
G

Grumble

E. Robert Tisdale said:
That's a lie.



I never said different.



Stephen Sprunk's does, in fact, use callback functions.
It is a classical example of the use of callback functions.

You have never provided a single reason
why you believe that these are not callback functions.
I submit that the reason is that you can't
and that you know that you can't.
You are simply trolling.

Please go away troll.

Bob,

(Can I call you Bob?)

You sound like a 12-year old.

Why don't you take a break from Usenet, and come back next year.
 
R

Richard Tobin

It's not usually used for the case
where a library gives you a function to call.

I never argued that the callback function was provided by the library.[/QUOTE]

Surely in the method case the object is analogous to the library. The
object provides a function pointer for you to call.

To regard this as a callback means considering "you" to be the object,
and the program using the object to be in the role of the library,
which is the wrong way round.

-- Richard
 
E

E. Robert Tisdale

Richard Tobin wrote:

E. Robert Tisdale wrote:



Surely, in the method case,

I'm not sure what you mean by "method case".
the object is analogous to the library.

No.
You might draw that analogy for an object of the *base* type
but *not* for an object of a type *derived* from the base type
by you -- the user of the base class (library).
The object provides a function pointer for you to call.

To regard this as a callback means considering "you" to be the object,

Well, it means the *you* provided the object which contains a pointer
to the callback function or to a table which contains pointers
to callback functions.
and the program using the object to be in the role of the library,
which is the wrong way round.

I think that you are confused.
Try giving us an example.
 
M

Michael Wojcik

At first I thought, "of course function pointers aren't used only for
callback functions." But after reading and thinking, I decided that
ultimately, if that function pointer is every actually used it is
effectively "calling back" to the function it points to.

When function pointers are used to implement a jump table, in what sense
are calls through them "back" to the functions they point to?

For example:

----- snip -----
#include <stdio.h>

void foo(void) { puts("foo"); }
void bar(void) { puts("bar"); }

void (*fptrs[])(void) = {foo, bar};

int main(void)
{
int c, i;

while ((c = getchar()) != EOF) {
switch (c) {
case 'f': i = 0; break;
case 'b': i = 1; break;
default: i = -1; break;
}
if (i >= 0 && i < sizeof fptrs / sizeof fptrs[0])
fptrs();
}

return 0;
}
----- snip -----

That's obviously a trivial example, but it's no different in principle
than a real program that uses an array of function pointers to perform
various operations based on input. Such programs typically compute an
index into the array, then call the function corresponding to that
index. It's equivalent to having a switch for each possible index
value and an explicit call to the corresponding function, but tidier.

When foo or bar is called, in what sense is that call "back" to
something? "Back" from where?

A function F acts as a callback when it belongs to domain A, domain A
makes F available to domain B, domain A calls into domain B, and
domain B calls F. Callback is a role that a function can satisfy. It
has nothing to do with function pointers whatsoever.

--
Michael Wojcik (e-mail address removed)

Duck: No secret what's worth a hoot ought to be kept quiet.
Pogo: Secrets is usually perty doggone fascinatin'.
Duck: Egg-zackly ... it's completely illogical to keep a secret secret.
Pogo: An' unfair. -- Walt Kelly
 
M

Mike Wahler

E. Robert Tisdale said:
You are a liar.
I never said any such thing.
I merely asked for an example of a function pointer
that is not used as a callback function.
So far, you have failed to provide one.

Here's a crumb for the troll:

void (*fp)(void); /* not used as a callback function */

Also note that no function pointer can be used as a
callback function, since a pointer is not a function,
nor is it convertible to one.


-Mike
 
M

Mike Wahler

Prashanth Ellina said:
"Mike Wahler" <[email protected]> wrote in message


shouldn't the above line be,
object.fp(&object);
?

Yes, to do it "OO style". Sorry about the typo.
However, the original form is still valid code
(defeating the purpose of the example,
but causing me to take a clean compile as
'what I meant'. :)).


-Mike
 
E

E. Robert Tisdale

Michael said:
Rufus said:
At first I thought, "of course function pointers aren't used only for
callback functions." But after reading and thinking, I decided that
ultimately, if that function pointer is every actually used
it is effectively "calling back" to the function it points to.

When function pointers are used to implement a jump table,
in what sense are calls through them
"back" to the functions they point to?

For example:

----- snip -----
#include <stdio.h>
// domain A:
void foo(void) { puts("foo"); }
void bar(void) { puts("bar"); }

void (*fptrs[])(void) = {foo, bar};
// domain B:
int main(int argc, char* argv[]) {
int c = EOF;

while ((c = getchar()) != EOF) {
int i = -1;
switch (c) {
case 'f': i = 0; break;
case 'b': i = 1; break;
default: i = -1; break;
}
if (0 <= i && i < sizeof fptrs/sizeof fptrs[0])
fptrs();
}

return 0;
}
----- snip -----

That's obviously a trivial example,
but it's no different in principle
than a real program that uses an array of function pointers
to perform various operations based on input.

Except that is lacks any motivation for doing so
besides to help you make your point.
Such programs typically compute an index into the array,
then call the function corresponding to that index.
It's equivalent to having a switch for each possible index value
and an explicit call to the corresponding function, but tidier.
When foo or bar is called,
in what sense is that call "back" to something? "Back" from where?
A function F acts as a callback
when it belongs to domain A, domain A makes F available to domain B,
domain A calls into domain B, and domain B calls F.

The problem with your example is that main(void) is a function
that could be viewed an domain B.
You could "disambiguate" this by moving

void (*fptrs[])(void) = {foo, bar};

inside the definition of function main(void).

Callback is a role that a function can satisfy.
It has nothing to do with function pointers whatsoever.

Correct.
In this example:

#include <stdio.h>

void f(const void (*fptrs[])(void)) { // domain B
int c = EOF;

while ((c = getchar()) != EOF) {
int i = -1;
switch (c) {
case 'f': i = 0; break;
case 'b': i = +1; break;
default: i = -1; break;
}
if (0 <= i && i < sizeof fptrs/sizeof fptrs[0])
fptrs();
}
}

void foo(void) { puts("foo"); }
void bar(void) { puts("bar"); }

int main(int argc, char* argv[]) { // domain A
const
void (*fptrs[])(void) = {foo, bar};
f(fptrs);
return 0;
}

foo(void) and bar(void) *are* callback functions
called back from function f(const void (*fptrs[])(void))
through the fptrs "jump table"
supplied by function main(int, char**).
 
M

Michael Wojcik

Except that is lacks any motivation for doing so
besides to help you make your point.

A fascinating observation. What part of the word "example" do you
not understand?

Or are you claiming that there are no real, nontrivial programs
which use this mechanism? If so, kindly prove it.
The problem with your example is that main(void) is a function
that could be viewed an domain B.

No it can't, since in my example nothing calls main except the
environment. The environment is not part of the program. Thus
there is no domain A in the program which can invoke main; thus
main cannot be domain B.
You could "disambiguate" this by moving

void (*fptrs[])(void) = {foo, bar};

inside the definition of function main(void).

This changes nothing. foo and bar are still not callbacks,
because they are not called "back" into anywhere. They are
called "forward", just as if they were called directly rather
than through a function pointer.

The jump table example is precisely an example of using a
function pointer for a "forward" call.
 
E

Eru

SenderX said:

I have been working on a short paper about OOC in the past few days; I
am not sure if it is ready for the revision of the experts that read
this newsgroup (no sarcasm there), but as the subject has come out, here
I go:

http://www.aditel.org/~ripolles/OOC.tar.gz

That .tar.gz archive contains a PostScript file with an explanation of
the method I propose (gathering from many suggestions read in many
different places); it also contains a couple of code samples.

Any contributions/corrections are welcome, but please be gentle! :)
 

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,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top