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


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

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-22 19:25 -0400
Organization A noiseless patient Spider
Message-ID <j2uoi5$jgn$1@dont-email.me> (permalink)
References <9bg0ojFsapU1@mid.individual.net>

Show all headers | View raw


On 8/22/2011 5:41 PM, Default User wrote:
> I've been working on a project that implements instrument drivers. The API
> uses operation codes to direct behavior. All the drivers are subclasses of a
> common ABC.  In my implementation, I used a method of mapping the op codes
> to handlers for that operation. That was accomplished with pointers to
> member functions.
>
> Someone asked me why I didn't have the handler storage and lookup in the
> base class. My initial response was that the pointers had different types.
> Then I got thinking that such a condition doesn't normally slow us down. So
> I created a test case (skipping the map and all).
>
> This worked, in that it built and seem to run ok. I'm not overly thrilled
> with the cast necessary to store the func pointer. Does anyone see a
> problem? Improvements?
>
>
> Brian
>
> ========= code =========
>
> #include<iostream>
>
> #define CALL_HANDLER(object,ptrToMember)((object).*(ptrToMember))

I'd probably tried to change this to

     CALL_HANDLER(o, p, args) ((o).*(p)) args

(see below for the use).

>
> class tbase
> {
> public:
>     tbase() : p(0)

Perhaps (a) it shouldn't be public (do you intend to instantiate this? 
It's an ABC, isn't it?) and (b) it should have the argument to init the 
'p' with (defautl to 0):

protected:
      tbase(HandlerPtr pp) : p(pp) {}

>     {
>     }

Move the typedef above the c-tor:

>     typedef  int (tbase::*HandlerPtr)(int);
> protected:
>     HandlerPtr p;
> };
> class test : tbase
> {
> public:
>     test()
>     {
>        p  = static_cast<HandlerPtr>(&test::f);

Shouldn't you use the initializer list instead of the assignment?  See FAQ.

>     }
>     int f(int i)
>     {
>        std::cout<<  "f: "<<  i<<  "\n";
>        return i;
>     }
>     void m()
>     {
>        CALL_HANDLER(*this, p)(1);

The use
          CALL_HANLDER((this, p, (1));

>     }
> };
>
> int main()
> {
>     test t;
>     t.m();
>     return 0;
> }

There is no real problem yet.  Now imagine that somebody thinks of 
deriving from your 'test' and providing their own handler?  How would 
they do it?  Or can that never happen?

Another possible solution is to actually have a pointer to function 
instead of pointer to member, and cast 'this' to the specific class, 
which is usually "safer", I think.  Not that I know much about that...

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