Pointer to Member Functions and Inheritance

Discussion in 'C++' started by none, Aug 8, 2004.

  1. none

    none Guest

    Hi,
    I have a base class with a pointer-to-member function
    variable. Then I have a derived class that needs to
    use that variable to call a member function (with the
    same arguments and return value type) in the derived
    class. I know in Visual C++ you can just cast the
    pointer to member function of the derived class to
    that of the base class and it will work, but is this
    legal by C++ standards?

    Thanks for any replies,
    steve

    Here's a sample source.

    #include<iostream>
    using namespace std;

    class base {
    protected :
    // pointer to member function variable
    typedef void (base::*function)(void);
    function ptr;
    public :
    // call the member function addressed by ptr
    virtual void CallFunction(void) { (this->*ptr)(); }
    public :
    // possible functions to call
    virtual void Function1(void) {
    cout << "Calling base::Function1..." << endl;
    }
    virtual void Function2(void) {
    cout << "Calling base::Function2..." << endl;
    }
    public :
    base() { ptr = &base::Function1; }
    virtual ~base() { ptr = 0 };
    };

    class derived : public base {
    public :
    void Function1(void) {
    cout << "Calling derived::Function1..." << endl;
    }
    void Function2(void) {
    cout << "Calling derived::Function2..." << endl;
    }
    public :
    derived() {
    // must cast member to base type, this legal?
    ptr = (function)&derived::Function1;
    }
    ~derived() {}
    };

    int main()
    {
    derived d;
    d.CallFunction();
    return 0;
    }
     
    none, Aug 8, 2004
    #1
    1. Advertising

  2. On Sat, 07 Aug 2004 23:36:57 GMT, none <> wrote:

    >Hi,
    >I have a base class with a pointer-to-member function
    >variable. Then I have a derived class that needs to
    >use that variable to call a member function (with the
    >same arguments and return value type) in the derived
    >class. I know in Visual C++ you can just cast the
    >pointer to member function of the derived class to
    >that of the base class and it will work, but is this
    >legal by C++ standards?


    You shouldn't need a cast, if I am reading the standard correctly. If
    the member is accessible (i.e. public or protected), then you should
    be able to use it directly.

    If a cast is necessary, you must cast the base class pointer to that
    of the derived class according to 4.11.2 of the standard. The base
    class may not be inaccessible, nor ambiguous, nor virtual.

    There is a footnote here which reads:

    "The rule [...] appears inverted compared to the rule for pointers to
    objects [...] This inversion is necessary to ensure type safety [...]"

    --
    Bob Hairgrove
     
    Bob Hairgrove, Aug 8, 2004
    #2
    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. Fraser Ross
    Replies:
    4
    Views:
    1,078
    Fraser Ross
    Aug 14, 2004
  2. Hicham Mouline
    Replies:
    0
    Views:
    450
    Hicham Mouline
    Apr 23, 2009
  3. Stephen Howe
    Replies:
    2
    Views:
    303
    Stephen Howe
    Nov 6, 2012
  4. somenath
    Replies:
    10
    Views:
    302
    James Kanze
    Jul 2, 2013
  5. somenath
    Replies:
    2
    Views:
    174
    somenath
    Aug 29, 2013
Loading...

Share This Page