I have a situation similar to Multiple inheritance + virtual function mess. I've replicated the code in a less complex environment to demonstrate what I'm confused about.
I want to know how C is executing (but not acessing) both B1::method and B2::method, which then in turn executes the inherited methods.
The only way I can see this as working (correctly), is because the parent class is the one that propagates the function call to the sub classes, so it is accessing the vtables of Bx directly instead of through C.
In either case, is it safe, or undefined behavior, what pitfalls etc; and what is maybe a better way to do this.
#include <iostream>
#include <vector>
class A {
static std::vector<A*> listeners;
public:
static void propagate();
protected:
A() {
listeners.push_back(this);
}
~A() {
for (std::vector<A*>::iterator it = listeners.begin(); it != listeners.end(); ++it) {
if (*it == this) {
listeners.erase(it);
break;
}
}
}
virtual void method()=0;
};
std::vector<A*> A::listeners;
void A::propagate() {
for (unsigned int i=0; i < listeners.size(); ++i) {
listeners[i]->method();
}
}
class B1 : public A {
protected:
B1() {}
~B1() {}
void method() {
B1inhmethod();
}
virtual void B1inhmethod() {}
};
class B2 : public A {
protected:
B2() {}
~B2() {}
void method() {
B2inhmethod();
}
virtual void B2inhmethod() {}
};
class C : public B1, public B2 {
public:
C() {}
~C() {}
void B1inhmethod() {
std::cout << "\nB1method in C";
}
void B2inhmethod() {
std::cout << "\nB2method in C";
}
};
int main() {
C myclass;
A::propagate();
return 0;
}
output:
B1inhmethod in C
B2inhmethod in C