I want to implement the pure virtual methods from an interface using the implementation
provided by an concrete class without having to call explicitly the method from the concrete class. Example:
class InterfaceA{
public:
virtual void foo() = 0;
};
class InterfaceB:public InterfaceA{
public:
virtual void bar() = 0;
};
class ConcreteA : public InterfaceA{
public:
virtual void foo(){}//implements foo() from interface
};
class ConcreteAB: public InterfaceB, public ConcreteA{
public:
virtual void bar(){}//implements bar() from interface
};
In this scenario, the compiler asks for a implementation of foo() in class ConcreteAB, because InterfaceB does not have it implemented and it inherited from InterfaceA.
There is a way to tell the compiler to use the implementation from ConcreteA without using a wrapper calling ConcreteA::foo()?
Make InterfaceA a virtual base class.
class InterfaceB : public virtual InterfaceA {
public:
virtual void bar() = 0;
};
class ConcreteA : public virtual InterfaceA {
public:
virtual void foo(){}//implements foo() from interface
};
You need virtual inheritance.
Interface A, at the top of the hierarchy, should be inherited virtually by all immediate subclasses.
class InterfaceB:public virtual InterfaceA{
public:
virtual void bar() = 0;
};
class ConcreteA : public virtual InterfaceA{
public:
virtual void foo(){}//implements foo() from interface
};
Related
This is more of a preferred style question when working in c++. I am looking for preferred standards around this.
Say I have an interface A
class AInterface {
public:
virtual ~AInterface() = default;
virtual void a() = 0;
};
And then I have another interface B that is built on top on A
class BInterface : public AInterface{
public:
virtual void b() = 0;
virtual void otherb() = 0;
};
My question is should I redeclare all the virtual functions from AInterface in B like so?
class BInterface : public AInterface{
public:
virtual void a() = 0;
virtual void b() = 0;
virtual void otherb() = 0;
};
No. You do not have to.
...and you should not, because it is unnecessary duplication without benefit. If you do, at least use the override keyword to avoid a mismatch in the declarations.
Imagine you change the base to
class AInterface {
public:
virtual ~AInterface() = default;
virtual void a(int) = 0;
};
and you forget to update BInterface, then whoops, now you have a void a() in BInterface that was not intended. If however you have override:
class BInterface : public AInterface{
public:
virtual void a() override = 0;
virtual void b() = 0;
virtual void otherb() = 0;
};
You would get a compiler error, because after the above refactoring void a() does not override a method from its base.
you would only re declare/define the virtual functions in subsequent classes if you intend to change their functionality from that of the base class. that said the examples you've given are both pure virtual classes so you wouldn't be able to instantiate them. But in general, one of the points of inheritance is that you don't have to continuously re-declare functions that do the same thing for derived classes.
I was wondering what was the best way to achieve code reuse by inheritance in a COM application.
The Problem
It came to my understanding that the virtual inheritance model of C++ cannot by used in a COM environment because it is not language independent.
In pure C++ a class hierarchy could look something like the following:
interface IBase {
virtual void BaseMethod() = 0;
};
interface IDerived : virtual IBase {
virtual void DerivedMethod() = 0;
};
class CBase : public virtual IBase {
public:
virtual void BaseMethod() override { /* Do something */ }
};
class CDerived : public IDerived, public CBase {
public:
virtual void DerivedMethod() override { /* Do something */ }
};
This results in the following hierarchy:
IDerived and CBase both inherit virtually from IBase.
Since virtual inheritance is not available in COM, the class hierarchy would rather look something like this:
interface IBase {
virtual void BaseMethod() = 0;
};
interface IDerived : IBase {
virtual void DerivedMethod() = 0;
};
class CBase : public IBase {
public:
virtual void BaseMethod() override { /* Do something */ }
};
class CDerived : public IDerived, public CBase {
public:
virtual void DerivedMethod() override { /* Do something */ }
};
This results in the following hierarchy:
At first glance, there seems to be a problem with the ambiguity of IBase. This can be easily resolved by implementing the IUnknown::QueryInterface method.
The real problem is, how can CDerived inherit the implemented methods of IBase from CBase, namely CBase::BaseMethod?
In the case of virtual inheritance, CBase::BaseMethod can be inherited by dominance, but without virtual inheritance this cannot happen: In CDerived, the method CBase::IBase::BaseMethod is defined, however the method IDerived::IBase::BaseMethod is not, resulting in the class CBase still being abstract and not thus not eligible for instantiation.
One way, to resolve this would be to override the method BaseMethod in CDerived once again:
class CDerived : public IDerived, public CBase {
public:
virtual void BaseMethod() override { CBase::BaseMethod(); }
virtual void DerivedMethod() override { /* Do something */ }
};
Does this have any performance implications? Is there a better way of achieving what I want? Doesn't the repeated override of the function in CDerived defeat the whole idea of inheritance?
ATL widely uses a technique that goes like this:
template <typename Itf>
class IBaseImpl : public Itf {
public:
void BaseMethod() override;
};
class CBase : public IBaseImpl<IBase> {};
class CDerived : public IBaseImpl<IDerived> {
public:
void DerivedMethod() override;
};
Now you have straight-line inheritance: IBase <- IDerived <- IBaseImpl<IDerived> <- CDerived.
The following code only works if you uncomment the line
virtual void FuncA() { ImplA::FuncA(); }
in class ImplB, otherwise I get compiler error:
cannot instantiate abstract class ... FuncA(void)' : is abstract
Question is why doesn't it get the implement for FuncA() from the inherited ImplA?
class InterfaceA {
public:
virtual void FuncA()=0;
};
class InterfaceB : public InterfaceA {
public:
virtual void FuncB()=0;
};
class ImplA : public InterfaceA {
public:
virtual void FuncA() { printf("FuncA()\n"); }
};
class ImplB : public ImplA, public InterfaceB {
public:
// virtual void FuncA() { ImplA::FuncA(); }
virtual void FuncB() { printf("FuncB()\n"); }
};
{
ImplB *b = new ImplB();
InterfaceA *A= b;
A->FuncA();
InterfaceB *B= b;
B->FuncB();
B->FuncA();
}
You've hit an instance of the "diamond" problem in multiple inheritance.
You'll need to use "virtual" inheritance (which amounts to adding the keyword virtual when inheriting)
The problem is that ImplB has two paths to the base class InterfaceA. However, your intention is that the interfaces do not provide any implementation. Thus, you need to indicate this to the compiler, so it can unify the pure virtual functions.
For a much better explanation:
http://www.cprogramming.com/tutorial/virtual_inheritance.html
I've modified your code to add virtual when you inherit from interfaces. Now it compiles, even with the line commented. Also note, I think you are missing virtual destructors, so you'll have some other problems down the line. This code compiles, without uncommenting FuncA.
#include <cstdio>
class InterfaceA {
public:
virtual void FuncA()=0;
};
class InterfaceB : public virtual InterfaceA {
public:
virtual void FuncB()=0;
};
class ImplA : public virtual InterfaceA {
public:
virtual void FuncA() { printf("FuncA()\n"); }
};
class ImplB : public ImplA, public virtual InterfaceB {
public:
// virtual void FuncA() { ImplA::FuncA(); }
virtual void FuncB() { printf("FuncB()\n"); }
};
int main()
{
ImplB *b = new ImplB();
InterfaceA *A= b;
A->FuncA();
InterfaceB *B= b;
B->FuncB();
B->FuncA();
}
Multiple inheritance isn't "mixins"
You can inherit from multiple classes that have methods with the same name but that doesn't make them the same.
The thing that inherits from a virtual class must implement the pure virtual functions of its parents.
If the method names weren't scoped, you could end up with combinations of parent classes that were mutually exclusive to inherit from because the implementations of the method with the shared name wouldn't be compatible.
I am a bit surprised that putting using ImplA::FuncA; in ImplB doesn't solve it, though: https://gcc.godbolt.org/
Lets say we have following hierarchy:
class Abstract
{
public:
virtual void foo() = 0;
};
class Base : public Abstract
{
public:
virtual void foo() override; //provides base implementation
};
class Derived : public Base
{
public:
virtual void foo() override; //provides derived implementation
};
If Base::foo() is ever called on the Derived object that object will desync and its data will be corrupted. It inherits Base's data structure and its manipulation but needs to perform additional operations so calling only the Base::foo() will omit these extra operations and as a result the Derived's state will be corrupted.
Therefore I would like to prevent direct call of Base implementation of foo so this:
Derived d;
d.Base::foo();
ideally, should give me a compile time error of some sorts. Or do nothing or otherwise be prevented.
However it might be I am violating the polymorphism rules and should use composition instead but that would require a lots of extra typing...
How about template method pattern:
class Abstract
{
public:
void foo() { foo_impl(); }
private:
virtual void foo_impl() = 0;
};
class Base : public Abstract
{
private:
virtual void foo_impl() override; //provides base implementation
};
class Derived : public Base
{
private:
virtual void foo_impl() override; //provides derived implementation
};
then
void test(Abstract& obj) {
obj.foo(); // the correct foo_impl() will be invoked
}
Derived d;
test(d); // impossible to call the foo_impl() of Base
You can explore the template method pattern. It allows for greater control of the execution of the methods involved.
class Abstract
{
public:
virtual void foo() = 0;
};
class Base : public Abstract
{
protected:
virtual void foo_impl() = 0;
public:
//provides base implementation and calls foo_impl()
virtual void foo() final override { /*...*/ foo_impl(); }
};
class Derived : public Base
{
protected:
virtual void foo_impl() override; //provides derived implementation
};
The pattern is seen in the iostreams library with sync() and pubsync() methods.
To prevent the direct calls and maintain the consistent state, you will need to get the final implementation of the foo method in the correct place in the stack. If the intent is to prohibit the direct call from the top of the hierarchy, then you can move the _impl methods up.
See also the non-virtual interface, the NVI pattern.
Bear in mind as well that the overriding methods do not have to have the same access specifier as the Abstract class. You could also just make the methods in the derived classes private or protected;
class Abstract
{
public:
virtual void foo() = 0;
};
class Base : public Abstract
{
virtual void foo() override; //provides base implementation
};
class Derived : public Base
{
virtual void foo() override; //provides derived implementation
};
Note: unless otherwise intended, changing the access specifier could be considered bad design - so basically if you do change the access specifier, there should should be a good reason to do so.
You can make all the foo() methods non-public, then have a non-virtual function in the Abstract class that simply calls foo.
Question
Why not to try virtual inheritance if it seems to solve my diamond inheritance problem below?
Briefing:
While learning C++, I came with some compiler errors for the following code:
#include <stdlib.h>
#include <iostream>
class IFoo{
public:
virtual void Hello() = 0;
};
class Foo : public IFoo{
public:
void Hello()
{
printf("Hello");
}
};
class IBar : public IFoo{
public:
virtual void HelloWorld() = 0;
};
class Bar : public IBar, public Foo{
public:
void HelloWorld()
{
Hello();
printf("World");
}
};
int main()
{
Bar b;
b.HelloWorld();
system("pause");
return 0;
}
I want Bar to implement abstract class IBar which has IFoo as a base class but then I want all the implementation of IFoo provided by Bar's second base class Foo.
I get 2 compiler errors (GCC 4.9.2): One related to ambiguities and another one about missing implementations for abstract class IFoo.
Then I found this question and got to meet the concept of virtual inheritance, which lead me to this page. Following the tutorial, I added virtual inheritance and all problems were gone:
class Foo : public virtual IFoo{...
class IBar : public virtual IFoo{...
But a user in the question suggest not to try virtual inheritance. Hence my question.
Your class
Bar
must override
Hello
otherwise class Bar also becomes purely abstract, because
Hello
is inherited all the way from
IFoo.
Whenever a class inherits an abtract base class it must implement all of the base class' pure virtual functions, otherwise the derived class itself also becomes abstract.
class Base{
public:
virtual void foo() = 0;
};
// this class is abstract, it doesn't implement Base::foo
class Derived1 : public Base{
public:
void fooDerived() {}
};
// this class is not abstract, it implements Base::foo
class Derived2 : public Base{
public:
void foo() {}
};
As suggested in another answer, in C++11 you can declare the function using the override keyword, which ensures that the function overrides a virtual function. Like this:
// this class is not abstract, it implements Base::foo
class Derived2 : public Base{
public:
void foo() override {}
};
Virtual inheritance solves another problem, the problem of ambiguity when inheriting from more than one base class which declares identical functions or members.
Your Hello() call in Bar::HelloWorld() is ambiguous. The compiler can't tell if you are trying to call Hello() from IBar or Foo. You can call Foo::Hello() or IBar::Hello() in it's place. That being said, your inheritance structure doesn't really make sense.
Your Bar class also needs to override Hello that it inherits from IBar.
If you have c++11, it would be a good idea to mark the virtual functions you are overriding with the override keyword:
#include <stdlib.h>
#include <iostream>
class IFoo{
public:
virtual void Hello() = 0;
};
class Foo : public IFoo{
public:
virtual void Hello() override
{
printf("Hello");
}
};
class IBar : public IFoo{
public:
virtual void HelloWorld() = 0;
};
class Bar : public IBar, public Foo{
public:
virtual void HelloWorld() override
{
Foo::Hello();
printf("World");
}
void Hello() override {}
};
int main()
{
Bar b;
b.HelloWorld();
system("pause");
return 0;
}
To answer your question on virtual inheritance, you could easily change your inheritance structure to something that still works that avoids a diamond pattern. The diamond pattern is rarely needed and is usually avoidable:
#include <stdlib.h>
#include <iostream>
class IFoo{
public:
virtual void Hello() = 0;
};
class Foo : public IFoo{
public:
virtual void Hello() override
{
printf("Hello");
}
};
class IBar {
public:
virtual void World() = 0;
};
class Bar : public IBar{
public:
virtual void World() override
{
printf("World");
}
};
class FooBar : public Foo, public Bar
{
public:
virtual void HelloWorld()
{
Hello();
World();
}
};
int main()
{
FooBar b;
b.HelloWorld();
system("pause");
return 0;
}