Re: static member function wrapper

From: Josh Sebastian (curien_at_cox.net)
Date: 11/11/03


Date: Tue, 11 Nov 2003 10:11:06 -0500

On Mon, 10 Nov 2003 18:57:38 -0800, forums_mp wrote:

> Josh Sebastian <curien@cox.net> wrote in message news:<pan.2003.11.10.23.03.01.619671@cox.net>...
>> On Mon, 10 Nov 2003 14:57:27 -0800, forums_mp wrote:
>>
>> > typedef void (TEST::*FUNCPTR)();
>> >
>> > static int TaskMain(TEST* obj, FUNCPTR p)
>> > {
>> > //(obj->(*p)()); // doesnt work .
>> > // p is of type int also not good ?
>>
>> ITYM
>>
>> obj->*p();
>>
>> No, they're not the same: ->* is a single operator.
>
> I was perusing one of my books in an attempt to understand this
> syntax. I could see we're trying to access a member function using a
> pointer but I'm at a loss on how this becomes a single operator.
>
> Greatly appreaciate the insight considering i was able to get
> something to work but here again I improve test code but dont think i
> full understand
> this
>
> (obj->*(obj->pFunc))();
>
>
> obj->pFunc = address of representative function

Address of a member function, yes.

> *(obj->pFunc) = deferenced or ...?

No.

  obj->*x

x is a pointer-to-member. The result is the member of obj that x points
to. In your example, x is a little complex, but the same idea applies.

> argument list () works outside ...

And the () function-call-operator invokes the function.
 
> trying to work through the syntax but as with most things mine here is
> a case of confusion ..
>
>
>
> class TEST
> {
> private:
> int spawned;
> static int bogus; // how would i initialize bogus

Outside the class, do

  int TEST::bogus = 42;

No, it doesn't matter that bogus is private.
      
> public:
> typedef void (TEST::*TESTPTR)();
>
> TESTPTR pFunc;
>
> TEST(TESTPTR func) : spawned(0), pFunc(func)
> {
> std::cout << " Constructor called " << std::endl;
> TEST::TaskMain(this);
> }
> ~TEST() { std::cout << " Destructor called " << std::endl; }
>
> static int TaskMain(TEST* obj)
> {
> (obj->*(obj->pFunc))(); // works but not really following
> this syntax

Well, how about this (which is equivalent):

  TESTPTR tp = obj->pFunc;
  obj->*tp();

Does that make it clearer?

> return 0;
> }
> void StartA() { std::cout << " A Started " << std::endl; }
> void StartB() { std::cout << " B Started " << std::endl; }
> void StartC() { std::cout << " C Started " << std::endl; }
> };

>> Josh



Relevant Pages

  • Re: Templates and access checking
    ... Compilers report that data is private, ... > exposure that the friend function creates. ... A friend member function would be ... can have a member function that simply returns a value of 'data'. ...
    (comp.lang.cpp)
  • Re: faster access private or public?
    ... >for the compiler to throw away a private member variable... ... >5) any member function that modifies the private variable must be private ... theoretically possible for private variables to allow extra ...
    (microsoft.public.vc.language)
  • how C++ works when initializing its class member?
    ... It looks to me everytime any member function is called, ... }; // class vector ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Use of static and extern
    ... I'm not aware of any semantic difference between initialization of a const ... like a "normal" member function does. ... >the static qualifier limit scope to the file in which the definition ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Which variable type?
    ... that it returns an object of type CommandBarControl.) ... It would be better to include a declaration like ... > The Item method syntax has the following object qualifier and part: ... An expression that specifies the position of a member ...
    (microsoft.public.word.vba.general)