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


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

Re: Fun with member-function pointers

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From "Default User" <defaultuserbr@yahoo.com>
Newsgroups comp.lang.c++
Subject Re: Fun with member-function pointers
Date Wed, 24 Aug 2011 09:33:13 -0500
Lines 108
Message-ID <9bkgdaF95nU1@mid.individual.net> (permalink)
References <9bg0ojFsapU1@mid.individual.net> <4e5351c3$0$6566$9b4e6d93@newsspool3.arcor-online.net> <9bhvopFfssU1@mid.individual.net> <j31l4o$iso$2@dont-email.me>
X-Trace individual.net MtVNjkmNXTJSmKs1LFQc0wq7dL+7A1Stkl+i5wRph6K0sAn8/x
Cancel-Lock sha1:GAxoPJdP1jLBeHAdtq43mxpMcxA=
X-Priority 3
X-MSMail-Priority Normal
X-Newsreader Microsoft Outlook Express 6.00.2900.5931
X-RFC2646 Format=Flowed; Response
X-MimeOLE Produced By Microsoft MimeOLE V6.00.2900.6090
Xref x330-a1.tempe.blueboxinc.net comp.lang.c++:9289

Show key headers only | View raw


"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote in message 
news:j31l4o$iso$2@dont-email.me...
> On 8/23/2011 8:36 AM, Default User wrote:
>>
>>> This is undefined behavior.
>>
>> I think that I'm convinced that you're correct. The example I had built
>> without issue and gave me the results I wanted, but as we know UB can do
>> that. Right up until someone changes something and then it doesn't.
>
> No. That poster is flat out incorrect.

Very interesting. Below will be a more complete example of what I'm 
proposing to do in the real code. That is, the managing of the handlers, 
storing the pointers, retrieving the correct one based on op code, and 
calling of the function pointer, take place in the base class. All the 
derived class has to do is set up the relationships between op codes and its 
member function pointers to implement the operation set for that particular 
module.

If this is still legal, then I'd be on pretty firm ground with my plan for 
the actual code.


Brian

#include <iostream>
#include <map>
#define CALL_HANDLER(object,ptrToMember)((object).*(ptrToMember))

class tbase
{
public:
   typedef int (tbase::*HandlerPtr)(int);
   void write(int data, int code)
   {
      std::map<int, HandlerPtr>::iterator it;
      it = handlers.find(code);
      if (it != handlers.end())
      {
         // Found, call the function pointer
         CALL_HANDLER(*this, it->second)(data);
      }
      else // unhandled code
      {
         // error handling
      }
   }
   void list()
   {
      std::map<int, HandlerPtr>::iterator it = handlers.begin();

      std::cout << "Supported Op codes:\n";
      while (it != handlers.end())
      {
         std::cout << it->first << "\n";
         it++;
      }
   }
protected:
   tbase() : baseDataStore(0)
   {
   }
   virtual ~tbase()
   {
   }
   std::map<int, HandlerPtr> handlers;
   int baseDataStore;
};

class test : public tbase
{
public:
   test() : testDataStore(0)
   {
      handlers[0] = static_cast<HandlerPtr>(&test::H0);
      handlers[4] = static_cast<HandlerPtr>(&test::H4);
   }
   virtual ~test()
   {
   }
   virtual int H0(int data)
   {
      baseDataStore = data;
      std::cout << "test::H0: " << baseDataStore << "\n";
      return data;
   }
   int H4(int data)
   {
      testDataStore = data;
      std::cout << "test::H4: " << testDataStore << "\n";
      return data;
   }
private:
   int testDataStore;
};

int main()
{
   test t;
   t.list();
   t.write(123, 0);
   t.write(456, 4);
   return 0;
}

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