Eclipse is reporting a bug on “new Listener()”, though this compiles and executes with g++. Note the classes have more data than listed here as this is trimmed down, so removing the seemingly empty classes is not an option. Is there something that I can add/change to make both Eclipse and g++ happy. I will not be able to change the Base* classes though.
class BaseIntf {
public:
virtual ~BaseIntf() {}
virtual void foo() = 0;
};
class BaseImpl: public virtual BaseIntf {
public:
virtual void foo() {}
};
class ListenerBaseInft: public virtual BaseIntf {
};
class Listener: public ListenerBaseInft, public BaseImpl {
};
int main(int argc, char *argv[]) {
// Eclipse:
// The type 'Listener' must implement the inherited pure virtual method 'BaseIntf::foo'
Listener* listener = new Listener();
listener->foo();
return 0;
}
C++ has a feature called dominance. Normally, when a class (say
A) is derived from two classes (sayA1andA2), each implementing the same virtual function (sayvoid f()), the derived class needs to implement that function as well. A pure virtual function counts as an implementation too.However, there is an exception: when
A1is itself derived fromA2, thenA1s implementation off()is said to dominate that ofA2, and the latter is excluded from consideration. Thus,Adoes not have to reimplementf().Your compiler probably fails to take that exception into account. It’s a compiler bug.
To work around it, define
fooinListener:which is ugly, but so is the compiler bug.