RE: [Dev-C++] Static members for callbacks
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ho...> - 2002-07-27 13:21:09
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Abhijit Shylanath
> Sent: Saturday, July 27, 2002 11:26 AM
> To: Dev-C++ Mailing List
> Subject: Re: [Dev-C++] Static members for callbacks
>
>
> > #include <iostream>
> >
> >
> > int main()
> > {
> > class A
> > {
> > public:
> > void something() { std::cout<<"Hello, new world!\n"; }
> > }a;
> >
> > void (A::*pf)()=&A::something;
>
> Okay, so this is how it's done... thanks. But this means that
> you can't use
> non-static methods for external callbacks (like Windows') directly.
Can you explain the above?
> Waitaminit... in what you've done above, aren't all objects'
> 'pf's set to
> A::something? What if I want one object's pf pointing to
> something, and
> another object's pf poiting to something_else?
Contrary to what you may thing a member function is unique for every
class. The only difference is that a *this pointer is passed to it in
addition to the other arguments and the compiler makes some additional
magic so as to behave as if there is one for each object (it uses *this
to permit you acccess the private members directly in the style
void A::something(void)
{
x++;
}
where x is a private member.
So if you have the above member function you can use void (A::*pf)() to
point to it and it will act the same "indepedently" on every object of
the class where it is called.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|