from base class calling methods defined in derived classes through pointer to member functions

Discussion in 'C++' started by Luca, Dec 19, 2003.

  1. Luca

    Luca Guest

    Hi, I have a quite complex question to ask you:

    I have defined a base class where I would like to have a map holding
    pointers to member functions defined in derived classes.

    To be more precise I would like my base class to have the following
    member:

    map<string, pointer_to_member_function> myClassMap;

    my base class also has a string member - say m_sValue - whose value
    can be the same of one of the string values contained in the map
    My base class should also have a public method, say "execute" that
    does the following:

    1.retrieve the map pair whose key has the same value as m_sValue
    2.dereference the poniter to member function contained in the second
    member of the map pair element, thus invoking the member function
    defined in the derived class

    Is that possible to do?

    Thank you

    Luca
     
    Luca, Dec 19, 2003
    #1
    1. Advertising

  2. Re: from base class calling methods defined in derived classes throughpointer to member functions

    Luca wrote:
    > Hi, I have a quite complex question to ask you:
    >
    > map<string, pointer_to_member_function> myClassMap;
    >
    > 1.retrieve the map pair whose key has the same value as m_sValue
    > 2.dereference the poniter to member function contained in the second
    > member of the map pair element, thus invoking the member function
    > defined in the derived class
    >
    > Is that possible to do?


    The problem is that you cant store memberfunctions of different classes
    in a map. Base::bar() and Derived::bar() have different types.

    Maybe as a workaround, you could do it this way:
    (Untested, just to show the idea...)

    class Base
    {
    public:
    typedef std::map<std::string, void (*)(Base*)> FunctionMap;
    string funcname;
    FunctionMap functions;

    void execute()
    {
    FunctionMap::iterator it = functions.find(funcname);
    if (it != functions.end())
    (it->second)(this);
    }
    };

    class Derived : public Base
    {
    void doSomething() {};

    public:
    static void execute(Base* base)
    {
    static_cast<Derived*>(base)->doSomething();
    }

    Derived() { functions["Derived"] = &execute }
    };

    hth

    Christoph
     
    Christoph Rabel, Dec 19, 2003
    #2
    1. Advertising

  3. Luca wrote in news::

    > Hi, I have a quite complex question to ask you:
    >
    > I have defined a base class where I would like to have a map holding
    > pointers to member functions defined in derived classes.
    >
    > To be more precise I would like my base class to have the following
    > member:
    >
    > map<string, pointer_to_member_function> myClassMap;
    >


    struct derived;

    typedef void (derived::*pointer_to_member_function)();

    > my base class also has a string member - say m_sValue - whose value
    > can be the same of one of the string values contained in the map
    > My base class should also have a public method, say "execute" that
    > does the following:
    >
    > 1.retrieve the map pair whose key has the same value as m_sValue


    1a. ? don't you want to check there is such a pair in the map ?

    map<string, pointer_to_member_function>::iterator ptr;
    ptr = myClassMap.find( myClassMap );

    > 2.dereference the poniter to member function contained in the second
    > member of the map pair element,


    (this->*(ptr->second))();

    > thus invoking the member function
    > defined in the derived class
    >
    > Is that possible to do?
    >


    Yes.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Dec 19, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Banaticus  Bart
    Replies:
    9
    Views:
    641
    Gary Labowitz
    May 7, 2004
  2. Siemel Naran
    Replies:
    4
    Views:
    837
    Micah Cowan
    Jan 12, 2005
  3. Replies:
    1
    Views:
    419
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    405
    Victor Bazarov
    May 23, 2007
  5. bgold
    Replies:
    12
    Views:
    1,047
    James Kanze
    Jan 1, 2008
Loading...

Share This Page