I am trying to figure out an interesting multiple inheritance issue.
The grandparent is an interface class with multiple methods:
class A
{
public:
virtual int foo() = 0;
virtual int bar() = 0;
};
Then there are abstract classes that are partially completing this interface.
class B : public A
{
public:
int foo() { return 0;}
};
class C : public A
{
public:
int bar() { return 1;}
};
The class I want to use inherits from both of the parents and specifies what method should come from where via using directives:
class D : public B, public C
{
public:
using B::foo;
using C::bar;
};
When I try to instantiate a D I get errors for trying to instantiate an abstract class.
int main()
{
D d; //<-- Error cannot instantiate abstract class.
int test = d.foo();
int test2 = d.bar();
return 0;
}
Can someone help me understand the problem and how to best make use of partial implementations?
You don't have diamond inheritance. The B and C base classes of D each have their own A base class subobject because they do not inherit virtually from A.
So, in D, there are really four pure virtual member functions that need to be implemented: the A::foo and A::bar from B and the A::foo and A::bar from C.
You probably want to use virtual inheritance. The class declarations and base class lists would look like so:
class A
class B : public virtual A
class C : public virtual A
class D : public B, public C
If you don't want to use virtual inheritance then you need to override the other two pure virtual functions in D:
class D : public B, public C
{
public:
using B::foo;
using C::bar;
int B::bar() { return 0; }
int C::foo() { return 0; }
};
You need to make your base classes virtual in order for them to inherit properly. The general rule is that all non-private member functions and base classes should be virtual UNLESS you know what you're doing and want to disable normal inheritance for that member/base.
Related
From what I have learnt about virtual inheritance, whenever we have a class A (the base class), classes B, C, D (all these three virtually inherit class A) then if there is a class derived which inherits from B, C and D classes is instantiated then only a single object of class A would be created.
I tried to reason about the virtual inheritance by taking different cases:
Case 1:
Assume the derived class inherits only from class B and C. Even then only a single object of class A gets instantiated, right?
I tried to verify this using this code and it certainly seems to be the case:
#include <iostream>
using namespace std;
class A
{
public:
int a;
};
class B: virtual public A
{
public:
int b;
};
class C: virtual public A
{
public:
int c;
};
class D: virtual public A
{
public:
int d;
};
class derived: public B, public C
{
public:
int y;
};
int main()
{
derived dObj;
cout<<&(dObj.B::a)<<'\n';
cout<<&(dObj.C::a)<<'\n';
return 0;
}
Output:
0x7ffc8adac1c0
0x7ffc8adac1c0
Case 2:
Assume the derived class inherits from class B, C and D, but class D doesn't inherit class A virtually. In this case, 2 objects of class A get instantiated, right? - One for B, C and one for D.
I tried to verify this using this code and it certainly seems to be the case:
#include <iostream>
using namespace std;
class A
{
public:
int a;
};
class B: virtual public A
{
public:
int b;
};
class C: virtual public A
{
public:
int c;
};
class D: public A
{
public:
int d;
};
class derived: public B, public C, public D
{
public:
int y;
};
int main()
{
derived dObj;
cout<<&(dObj.B::a)<<'\n';
cout<<&(dObj.C::a)<<'\n';
cout<<&(dObj.D::a)<<'\n';
return 0;
}
Output:
0x7ffd512429c8
0x7ffd512429c8
0x7ffd512429bc
Now here is my question:
Are my observations correct? That is, instead of just 3 classes - B, C, D, let's assume we have n classes that inherit class A, if out of them, m (1<=m<=n) classes are inherited by another class named derived, and if out of those m classes only k (0<=k<=m) classes inherit class A virtually then the number of objects of class A that get instantiated when instantiating the derived class would be = 1+m-k (if k!=0) and m (when k=0), right?
From the content written in the cppreference website it is indeed the case:
See the first example under the virtual base classes section.
class A has a pure virtual method read()
class B has an implemented virtual method read()
I have a class C that inherits A and B
Can this happen?
What I'm trying to achieve is that the two base classes A and B complement each other.
So C read() method would actually call class B read()
class A {
virtual int get_data() = 0;
void print() {
log(get_data());
}
}
class B {
virtual int get_data() {
return 4;
}
}
class C : public A, public B {
}
C my_class;
my_class.print(); // should log 4;
I'm not on my computer nor will have opportunity in the next couple of weeks so I can't test this... but I'm designing the architecture and needed to know if this is possible and if not.. how can this be accomplished!
Can multiple base classes have the same virtual method?
Can this happen?
Yes.
So C read() method would actually call class B read()
That doesn't happen automatically. A member function of base doesn't override a function of an unrelated base.
You can add an override to C:
class C : public A, public B {
int get_data() override;
}
This overrides both A::get_data and B::get_data. In order to "actually call class B read()", you can indeed make such call:
int C::get_data() {
return B::get_data();
}
Or... that would be possible if you hadn't declared B::get_data private.
Overriding a function in another base without explicitly delegating in derived is possible if you change your hierarchy a bit. In particular, you need a common base, and virtual inheritance:
struct Base {
virtual int get_data() = 0;
};
struct A : virtual Base {
void print() {
std::cout << get_data();
}
};
struct B : virtual Base {
int get_data() override {
return 4;
}
};
struct C : A, B {};
Consider the following example:
class A{
public: virtual void hello() = 0;
};
class B: public A{};
class C {
public: void hello(){
cout<<"Hi";
}
};
class D: public B, public C{};
The idea is that I would like to inject the implementation of hello into D through C. This doesn't seem to work unless I make C inherit from A too. Since that leads to diamond inheritance, I end up using virtual inheritance.
Is there any alternative to forcing an implementation into a derived class, without disturbing the abstract classes A and B here?
EDIT: I want a solution where I don't need to explicitly write code within D. This is because, I have many classes like D having the same implementation, which is exactly why I would like to push that implementation up to some class from which all of them inherit.
You can rewrite C as a template class that inherits from it's template argument and then derive D from C.
template <class Base>
class C : public Base {
public: void hello(){
cout<<"Hi";
}
};
class D: public C<B> {};
You can consider static inheritance/ policy classes when you need to inject an outside method into a class hierarchy. Note that injecting the method usually means that the method does not have the same name as an existing virtual in the class hierarchy (if it does, you are forced to use virtual inheritance or explicitly call with the scope :: or insert it in the class hierarchy). I called the method externalHello here.
The other options work fine as well but they conceptually point more to the fact that the injected method is not really an abstract method that could be used outside of this class hierarchy but should have been part of it in the first place.
class A
{
public:
virtual void hello() = 0;
};
class B: public A
{
public:
void hello()
{
cout<<"Hi from B" << endl;
};
};
template<typename T> class C1
{
public:
void injectedMethodUnrelatedToClassHierarchy()
{
cout<<"Hi from C1 with unrelated method" << endl;
};
void externalHello()
{
static_cast<T*>(this)->hello(); // will still call hello in B
injectedMethodUnrelatedToClassHierarchy(); // this will call hello here
};
};
class D: public B, public C1<D>{};
With client code:
D dx;
dx.hello();
dx.externalHello();
dx.injectedMethodUnrelatedToClassHierarchy();
It may work
#include<iostream>
using namespace std;
class A
{
public:
virtual void hello() = 0;
};
class C {
public: void hello(){
cout<<"Hi\n";
}
};
template<typename T>
class B: public A, public T
{
public:
void hello() override{ //override A::hello()
//do other staff
T::hello(); // call C::hello()
}
};
class D: public B<C>{
};
int main()
{
D d;
d.hello();
return 0;
}
considering following code:
#include <stdio.h>
struct ITimer {
virtual void createTimer() = 0;
};
class A : public ITimer
{
public:
void showA() {
printf("showA\n");
createTimer();
}
};
class B : public ITimer
{
public:
void showB() {
printf("showB\n");
}
void createTimer() {
printf("createTimer");
}
};
class C: public A, public B
{
public:
void test() {
showA();
showB();
}
};
int main()
{
C c;
c.test();
return 0;
}
I need to use interface ITimer in class A, but the method is implemented in class B. So I inherited the interface in A, but the compiler is not happy with it:
test.cc
test.cc(38) : error C2259: 'C' : cannot instantiate abstract class
due to following members:
'void ITimer::createTimer(void)' : is abstract
test.cc(5) : see declaration of 'ITimer::createTimer'
How could I use the interface in baseclass A while its method is implemented in class B.
Thanks.
Inheritance is the base-class of all evil.
A nor B are ITimers
A doesn't even implement the pure virtual, so it cannot be instantiated. Therefore, inheriting from A makes C abstract too (cannot be instantiated).
You don't want to use inheritance here. See
Liskov Substitution Principle ("is-a" rule)
In this case, the dreaded diamond-of-death hierarchy could be fixed by adding virtual:
class A : public virtual ITimer
//...
class B : public virtual ITimer
See it Live on IdeOne. I don't recommend this, though. Consider fixing the design.
See also Diamond inheritance (C++)
I have not programmed in c++ in a long time and want some simple behavior that no amount of virtual keywords has yet to produce:
class Base {
public:
int both() { return a(); }
};
class Derived : public Base {
protected:
int a();
};
class Problem : public Derived {
};
Problem* p = new Problem();
p.both();
Which gives me a compile-time error. Is this sort of behavior possible with c++? Do I just need forward declaration? Virtual keywords on everything?
No. You will have to use a pure virtual a in base.
class Base {
virtual int a() = 0;
int both() {
return a();
}
};
You should declare the a() function as a pure virtual method in the Base class.
class Base {
int both() {
return a();
}
virtual int a()=0;
};
Then implement the a() method in the Derived class
class Derived : public Base {
int a(){/*some code here*/}
};
And finally, Problem class doesn't see the both() method, since its private in Base. Make it public.
class Base {
public:
int both() {
return a();
}
};
Your function both() is private by default. Try:
class Base {
public:
int both() {
// ...
(In the future, it would be helpful if you tell us what the actual error message was.)
You need a() to be declared in class Base, otherwise the compiler doesn't know what to do with it.
Also, both() is currently a private method (that's the default for classes), and should be made public in order to call it from main.
You have multiple problems in your code :
unless you declare them public or protected, elements of a class are private as a default.
you need a virtual keyword to define a virtual function that would be callable in a parent.
new returns a pointer to Problem.
Here's a complete working code based on your test :
class Base {
protected:
virtual int a()=0;
public:
int both() {
return a();
}
};
class Derived : public Base {
private :
int a()
{
printf("passing through a!");
return 0;
}
};
class Problem : public Derived {
};
int main(void)
{
Problem* p = new Problem();
p->both();
}
tested on CodePad.
As others point out, you need to declare a() as pure virtual method of Base and change access to public to make your snippet work.
Here is another approach possible in c++: instead of virtual functions, you can use static polymorphism via the Curiously recurring template pattern:
template <class D>
class Base : public D
{
public:
int both() { return D::a(); }
};
class Derived : public Base<Derived>
{
public:
int a();
};
I'm posting this approach since you're asking what is possible in c++. In practice, virtual methods are most often a better choice because of their flexibility.