Re: gcc oriented questions



Am 08 Aug 2001 12:59:12 -0400 schrieb Paul Davis:
> In message <997278074 14844 10 camel master>you write:
> >Am 08 Aug 2001 15:03:15 +0200 schrieb David Robin:
> >> hi,
> >> 
> >> here are two different questions:
> >> - why are callback functions defined as 'static'? Is there an obligation to 
> >do so? Does it makes my app faster?
> >
> >Er, I guess you talking about static class members in C++ ?
> >A static class member function behaves like an ordinary C function,
> >e.g. it has no implicit "this"-pointer. Since the GTK+ C-API knows
> >nothing about C++, you have to declare the callback functions static.
> 
> to clarify a little (since when i first started with C++, i found this
> very hard to understand): a pointer-to-a-member-function is a
> *totally* different kind of entity in a C++ program than a
> pointer-to-a-function. you really need to get this firmly established
> in your mind and never confuse the two. the things you can do with one
> of them cannot be done with the other, and vice versa.
> 
> there is a small hint of this in C++'s syntax:
> 
>       void (SomeClass::*ptr_to_member)(void) func;
>       SomeClass *some_object;
> 
>       (some_object->*)(func);
> 
> the "->*" operator is unique to C++ and there is no corresponding
> equivalent operator in C. 
> 
> so, by declaring functions as "static", you convert pointers to them
> >from ptr-to-member into ptr-to-function. this makes it possible for C
> code to call them correctly.

Yep, you're right, my explanation wasn't very convincing.

But I think for a beginner we shouldn't start with the
function-pointer point of view at the first place. I'll
try to summarize:

When you declare this in C++:

class MyClass {
    mymember (int foo, char bar);
};

it will basically be translated into an ordinary function:

MyClass_mymember (MyClass* this, int foo, char bar);

And that's the whole point: You have an invisible first
parameter, because the member function has to know for
which object it was called.

I think we're getting slightly off-topic now ;)

C'ya,
--Daniel





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]