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]

Can a base class figure out if any virtual functions were overwriten?


In the following code, I would expect to discover that M1 was changed. But
it
doesn't happen.

Then I ask, is there any portable way to discover in the base class
whether or
not each of the virtual functions were overwritten in derived classes?

#include <iostream>

class Interface
{//this is the test case
public:
    virtual void M1(void) = 0;
    virtual void M2(void) = 0;
};

class Base : public Interface
{
public:
    // standard implementation for both virtuals
    void M1(void);
    void M2(void);


    void Check(void);
};

void Base::M1(void)
{
}

void Base::M2(void)
{
}


class Derived : public Base
{
    // overwrite one of the virtuals
    void M1(void);
};

void Derived::M1(void)
{
}


void Base::Check(void)
{
    // check if the virtuals remained unchanged or were overwritten
    if (M1 != Base::M1)
        std::cout << "M1 different\n";

    if (M2 != Base::M2)
        std::cout << "M2 different\n";
}


void main(void)
{
    Derived d;

    // check on an object of the derived class
    d.Check();
}




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