Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #9230

Re: Fun with member-function pointers

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: Fun with member-function pointers
Date 2011-08-23 08:04 -0400
Organization A noiseless patient Spider
Message-ID <j30516$jk7$1@dont-email.me> (permalink)
References <9bg0ojFsapU1@mid.individual.net> <4e535238$0$3087$7b1e8fa0@news.nbl.fi>

Show all headers | View raw


On 8/23/2011 3:09 AM, Juha Nieminen wrote:
> Default User<defaultuserbr@yahoo.com>  wrote:
>> #include<iostream>
>>
>> #define CALL_HANDLER(object,ptrToMember)((object).*(ptrToMember))
>>
>> class tbase
>> {
>> public:
>>    tbase() : p(0)
>>    {
>>    }
>>    typedef  int (tbase::*HandlerPtr)(int);
>> protected:
>>    HandlerPtr p;
>> };
>> class test : tbase
>> {
>> public:
>>    test()
>>    {
>>       p  = static_cast<HandlerPtr>(&test::f);
>>    }
>>    int f(int i)
>>    {
>>       std::cout<<  "f: "<<  i<<  "\n";
>>       return i;
>>    }
>>    void m()
>>    {
>>       CALL_HANDLER(*this, p)(1);
>>    }
>> };
>
>    My knowledge of the C++ standard fails me at this point. Is it really
> legal to store a method pointer of the derived class type into a method
> pointer variable of the base class type, and then call it like that?
>
>    (I'm pretty certain that if the base class tried to call the method
> through the pointer, using itself as the object, that would be UB (or
> similar). However, in this case it's the derived class that is doing the
> calling, so I'm not completely sure anymore.)

Analysing...  Analysing...

My guess is that this "round-trip" (store in the base, but call in the 
derived where the member function actually exists) might be actually a 
bad idea, and here is why.  The 'static_cast' used to convert to 'p' 
*can* change the pointer itself (adjust or whatever).  Calling the 
member from inside the derived class does not change the fact that the 
type of the pointer is "member of 'tbase'".  Only an implicit cast to 
"member of 'test'" could bring it back, so to speak.  IOW, the 'm' 
function should contain the declaration and use of test::*, converted 
from 'p', something like:

     void m()
     {
         void (test::*pp)(int) = p;
         CALL_HANDLER(*this, pp)(1);
     }

Then the conversions (to pointer to member of base, to pointer to member 
of derived) are mutually cancelling.

V
-- 
I do not respond to top-posted replies, please don't ask

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Fun with member-function pointers "Default User" <defaultuserbr@yahoo.com> - 2011-08-22 16:41 -0500
  Re: Fun with member-function pointers Victor Bazarov <v.bazarov@comcast.invalid> - 2011-08-22 19:25 -0400
    Re: Fun with member-function pointers "Default User" <defaultuserbr@yahoo.com> - 2011-08-22 19:06 -0500
  Re: Fun with member-function pointers Juha Nieminen <nospam@thanks.invalid> - 2011-08-23 07:09 +0000
    Re: Fun with member-function pointers Victor Bazarov <v.bazarov@comcast.invalid> - 2011-08-23 08:04 -0400
      Re: Fun with member-function pointers Juha Nieminen <nospam@thanks.invalid> - 2011-08-23 13:34 +0000
        Re: Fun with member-function pointers Victor Bazarov <v.bazarov@comcast.invalid> - 2011-08-23 13:02 -0400
      Re: Fun with member-function pointers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-23 19:12 -0700
    Re: Fun with member-function pointers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-23 18:51 -0700
  Re: Fun with member-function pointers Marcel Müller <news.5.maazl@spamgourmet.com> - 2011-08-23 09:07 +0200
    Re: Fun with member-function pointers Juha Nieminen <nospam@thanks.invalid> - 2011-08-23 10:09 +0000
      Re: Fun with member-function pointers Marcel Müller <news.5.maazl@spamgourmet.com> - 2011-08-23 15:04 +0200
    Re: Fun with member-function pointers "Default User" <defaultuserbr@yahoo.com> - 2011-08-23 10:36 -0500
      Re: Fun with member-function pointers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-23 18:46 -0700
        Re: Fun with member-function pointers "Default User" <defaultuserbr@yahoo.com> - 2011-08-24 09:33 -0500
          Re: Fun with member-function pointers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-24 08:32 -0700
            Re: Fun with member-function pointers "Default User" <defaultuserbr@yahoo.com> - 2011-08-24 11:15 -0500
              Re: Fun with member-function pointers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-24 09:30 -0700
      Re: Fun with member-function pointers Marcel Müller <news.5.maazl@spamgourmet.com> - 2011-08-24 12:27 +0200
        Re: Fun with member-function pointers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-24 06:46 -0700
          Re: Fun with member-function pointers Marcel Müller <news.5.maazl@spamgourmet.org> - 2011-08-24 18:57 +0200
            Re: Fun with member-function pointers Paul <pchristor@yahoo.co.uk> - 2011-08-24 13:08 -0700
              Re: Fun with member-function pointers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-24 13:44 -0700
                Re: Fun with member-function pointers Paul <pchristor@yahoo.co.uk> - 2011-08-25 04:23 -0700
                Re: Fun with member-function pointers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-25 07:31 -0700
                Re: Fun with member-function pointers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-25 08:21 -0700
                Re: Fun with member-function pointers Paul <pchristor@yahoo.co.uk> - 2011-08-25 10:50 -0700
                Re: Fun with member-function pointers Leigh Johnston <leigh@i42.co.uk> - 2011-08-25 13:57 +0100
                Re: Fun with member-function pointers Leigh Johnston <leigh@i42.co.uk> - 2011-08-25 14:08 +0100
                Re: Fun with member-function pointers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-25 07:32 -0700
                Re: Fun with member-function pointers Leigh Johnston <leigh@i42.co.uk> - 2011-08-25 15:45 +0100
                Re: Fun with member-function pointers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-25 07:48 -0700
    Re: Fun with member-function pointers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-23 18:43 -0700
  Re: Fun with member-function pointers Paul <pchristor@yahoo.co.uk> - 2011-08-23 03:49 -0700
    Re: Fun with member-function pointers Paul <pchristor@yahoo.co.uk> - 2011-08-23 06:44 -0700
  Re: Fun with member-function pointers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-23 18:28 -0700
  Re: Fun with member-function pointers Jeff Flinn <TriumphSprint2000@hotmail.com> - 2011-08-25 11:29 -0400

csiph-web