Answer the question
In order to leave comments, you need to log in
How to implement inheritance from the interfaces branch and the base implementation of the root interface?
Let's assume that there is a certain set of public interfaces (exported by the library):
class IA {
public:
virtual void f1() = 0;
};
class IB : public IA {
public:
virtual void f2() = 0;
};
class A : public IA {
public:
void f1() {
std::cout << "f1" << std::endl;
}
};
// Этот код не компилируется
class B : public IB, public A
{
public:
void f2() {
std::cout << "f2" << std::endl;
}
};
int main(int argc, char* argv[])
{
B b;
b.f1();
b.f2();
return 0;
}
Answer the question
In order to leave comments, you need to log in
I found the answer to the question
https://isocpp.org/wiki/faq/multiple-inheritance#m...
I learned that not only functions are virtual, but also inheritance.
Thank you.
Final code:
class IA {
public:
virtual void f1() = 0;
};
class IB : virtual public IA { // !!! VIRTUAL !!!
public:
virtual void f2() = 0;
};
class A : virtual public IA { // !!! VIRTUAL !!!
public:
void f1() {
std::cout << "f1" << std::endl;
}
};
class B : public IB, public A // А вот тут виртуальность необязательна.
{
public:
void f2() {
std::cout << "f2" << std::endl;
}
};
int main(int argc, char* argv[])
{
B b;
b.f1();
b.f2();
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question