Ryan's District
November 22, 2008, 02:49:04 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Ryan's District Lottery: Claim your ticket or check
Jackpot details  
 
   Home   Help Search Chat Calendar Chess Shop Login Register  
Digg This!
Pages: [1]   Go Down
  Send this topic  |  Print  
Author Topic: C++ tips - Menu item callbacks with templates  (Read 4809 times)
0 Members and 1 Guest are viewing this topic.
ben2ong2
Quality Poster
Paid
Hero Member
*****

Reputation: 17
Offline Offline

Gender: Male
Posts: 2374
9976.80 RD$

View Inventory
Send Money to ben2ong2

View Profile Awards
« on: October 07, 2006, 03:04:43 AM »

RESPONSE: Scott Meyers, sdm@cs.brown.edu, 30 Apr 93

    class BaseMenuItem {
    public:
        virtual void invoke() const = 0;
    };

    template<class Class, class MemFuncPtr, class Param>
    class MenuItem: public BaseMenuItem {
    private:
        Class&      object;
        MemFuncPtr  function;
        Param&      parameter;

    public:
        MenuItem(Class& obj, MemFuncPtr mfp, Param& p):
          object(obj), function(mfp), parameter(p) {}

        virtual void invoke() const { object.*mf(p); }
    };

Now you can create a MenuItem object for any class, member function, and
parameter you like, with the proviso that the member function must have
exactly one formal parameter.

It seems to me like this should work, but as I said, I haven't tried it
yet.  Comments?



RESPONSE: Stephen G. Edwards <edwards@kong.gsfc.nasa.gov>

Yes, it does work.  You've also just inadvertently solved a big problem I was
having trying to use this technique to implement a callback mechnism for
Motif.  I constrained my template a bit more like this:

    template<class Class>
    class MenuItem: public BaseMenuItem {
    private:
        Class&      object;
        void (Class::*function)(FixedType p);
        FixedType&      parameter;

    public:
        MenuItem(Class& obj, void (Class::*mfp)(FixedType), FixedType& p):
          object(obj), function(mfp), parameter(p) {}

        virtual void invoke() const { object.*mf(p); }
    };

The compiler choked when instantiating the MenuItem constructor.  I learned
there is a bug in cfront 3.0.1 that causes it to incorrectly reject the mfp
argument.  This should do the job, though.  Thanks!
Logged

You are not allowed to view links.
Register or Login
Free Paid Survey & Home Business Resources.

You are not allowed to view links.
Register or Login
Free Article Directory | Quality Content
Ryan's District
« on: October 07, 2006, 03:04:43 AM »

 Logged
Pages: [1]   Go Up
  Send this topic  |  Print  
 
Jump to:  

Archive - WAP2 - WAP - imode
Sponsors: RAYAN.tv
-

Powered by MySQL Powered by PHP Powered by SMF 1.1.7 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.231 seconds with 30 queries.

Google visited last this page Yesterday at 07:51:32 AM