This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Problems invoking a member function through pointers.




In the following code I would expect the output to be:
Base::M1
Base::M1
Base::M2
Base::M2

To my surprise, the last line of the output says
Derived::M2

Does the standard mandate this behaviour?
I've tried with both Microsoft compiler and egcs.

thanx



#include <iostream>

class Interface
{
public:
    virtual void M1(void) = 0;
    virtual void M2(void) = 0;
};

class Base : public Interface
{
public:
    void M1(void){ std::cout << "Base::M1\n"; }
    void M2(void){ std::cout << "Base::M2\n"; }


    void Check(void);
};



class Derived1 : public Base
{
public:
    void M2(void){ std::cout << "Derived::M2\n"; }
};

void Base::Check(void)
{
    void (Base::*pfn1)(void) = &Base::M1;
    Base::M1();
    (this ->* pfn1)();

    void (Base::*pfn2)(void) = &Base::M2;
    Base::M2();
    (this ->* pfn2)();
}



void main(void)
{
    Derived1 d1;

    d1.Check();
}




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]