I have a Base class A, 2 childs B and C that both inherits from A, and I have a class D that inherits from both B and C in which I want to take _a from B and _b from C without having to do a virtual method for print_a and print_b if possible
# include <iostream> class A { public: A(void) : { } ~A(void) { } inline void print_a(void) { std::cout << _a << std::endl; } inline void print_b(void) { std::cout << _b << std::endl; } protected: int _a; int _b; }; class B : virtual A { public: B(int a) : A() { _a = a, _b = 0; } ~B(void) { } protected: using A::_a; using A::_b; }; class C : virtual A { public: C(int b) : A() { _a = 0, _b = b; } ~C(void) { } protected: using A::_a; using A::_b; }; class D : virtual public A, private B, private C { public: D(void) : A(), B(5), C(5) { } ~D(void) { } private: using B::_a; using C::_b; }; int main(void) { D foo; foo.print_a(); foo.print_b(); return (0); } My D class is inheriting methods print_a and print_b from A and I want it to inherit _a = 5 from B and _b = 5 from C, but it doesn't work, it outputs:
0 5 Instead of:
5 5
class D : virtual public A, private C, private Bbut you are playing with fire if reordering things changes the output. Here is a sample with that change: onlinegdb.com/XOMzDDI8f