I have defined an interface class A which defines some basic functions. In my implementation I have a base class A0 which implements this interface and from this base class I have derived several other classes in a hierarchy.
#include <iostream>
#include <string>
class IContainer
{
public:
IContainer() {};
virtual ~IContainer() {};
virtual void loadDefaults() = 0;
virtual void storeDefaults() = 0;
virtual bool open() = 0;
virtual bool close() = 0;
};
class IContainerReader
{
public:
IContainerReader() {};
virtual ~IContainerReader() {};
virtual bool read() = 0;
};
class IContainerWriter
{
public:
IContainerWriter() {};
virtual ~IContainerWriter() {};
virtual bool write() = 0;
};
class ContainerBase : public IContainer
{
public:
ContainerBase() {}
virtual ~ContainerBase() {}
void loadDefaults() {}
void storeDefaults() {}
};
class CSVBase : public ContainerBase
{
public:
CSVBase() {}
virtual ~CSVBase() {}
void setFilename() {}
bool open() { return true; }
bool close() { return true; }
};
class CSVReader : public CSVBase, public IContainerReader
{
public:
CSVReader() {}
virtual ~CSVReader() {}
bool read() { return true; }
};
class CSVWriter : public CSVBase, public IContainerWriter
{
public:
CSVWriter() {}
virtual ~CSVWriter() {}
bool write() { return true; }
};
int main(int argc, char *argv[])
{
CSVReader r;
CSVWriter w;
IContainerReader *ir = &r;
IContainerWriter *iw = &w;
ir->open();
iw->open();
ir->read();
iw->write();
ir->close();
iw->close();
return 0;
}
As you can see I defined a IContainerReader and a IContainerWriter class which defines special functions only relevant to the respective implementation.
But now I have a problem, because I want to be sure that a Reader or a writer always has the container base as well. So the logical solution would be to derive IContainerReader/-Writer from IContainer. But when I do this, thge compiler complains, because it expects that now a Reader/Writer object implements again the base functions as well, which are already defined via the base class. But if I let IContainerReader not derive from IContainer a pointer to one of those objects is not guruanteed to have the IContainer functionality as well.
If I try to compile it like this I get errors, because IContainerReader is not a IContainer
||=== Build: Debug in CPPMingW (compiler: GNU GCC Compiler) ===|
D:\src\c\Tests\CPPMingW\main.cpp||In function 'int main(int, char**)':|
D:\src\c\Tests\CPPMingW\main.cpp|83|error: 'class IContainerReader' has no member named 'open'|
D:\src\c\Tests\CPPMingW\main.cpp|84|error: 'class IContainerWriter' has no member named 'open'|
D:\src\c\Tests\CPPMingW\main.cpp|89|error: 'class IContainerReader' has no member named 'close'|
D:\src\c\Tests\CPPMingW\main.cpp|90|error: 'class IContainerWriter' has no member named 'close'|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 5 second(s)) ===|
However, if I derive IContainerReader from IContainer as it should be, I get the following errors:
||=== Build: Debug in CPPMingW (compiler: GNU GCC Compiler) ===|
D:\src\c\Tests\CPPMingW\main.cpp||In function 'int main(int, char**)':|
D:\src\c\Tests\CPPMingW\main.cpp|78|error: cannot declare variable 'r' to be of abstract type 'CSVReader'|
D:\src\c\Tests\CPPMingW\main.cpp|58|note: because the following virtual functions are pure within 'CSVReader':|
D:\src\c\Tests\CPPMingW\main.cpp|11|note: virtual void IContainer::loadDefaults()|
D:\src\c\Tests\CPPMingW\main.cpp|12|note: virtual void IContainer::storeDefaults()|
D:\src\c\Tests\CPPMingW\main.cpp|14|note: virtual bool IContainer::open()|
D:\src\c\Tests\CPPMingW\main.cpp|15|note: virtual bool IContainer::close()|
D:\src\c\Tests\CPPMingW\main.cpp|79|error: cannot declare variable 'w' to be of abstract type 'CSVWriter'|
D:\src\c\Tests\CPPMingW\main.cpp|67|note: because the following virtual functions are pure within 'CSVWriter':|
D:\src\c\Tests\CPPMingW\main.cpp|11|note: virtual void IContainer::loadDefaults()|
D:\src\c\Tests\CPPMingW\main.cpp|12|note: virtual void IContainer::storeDefaults()|
D:\src\c\Tests\CPPMingW\main.cpp|14|note: virtual bool IContainer::open()|
D:\src\c\Tests\CPPMingW\main.cpp|15|note: virtual bool IContainer::close()|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 6 second(s)) ===|
So the compiler expects me to reiplment all the functions from the base classes in the derived class as well.
So is there a solution to this, without having to define all these functions in the Reader/Writer class again? Of course I could implement dummies which just go down to the base class, but I consider this a bit clumsy and unneccessary overhead and I hope that there may be a better solution for this.
I hope I got it right. You have a kind of diamond problem. You inherit from the IContainer via two paths .
Normally in such cases you would have two instances of IContainer created per one instance of CSVReader, and the calls to IContainer methods would be ambiguous. In your case the path through IContainerReader does not have defined the mentioned functions. Virtual inheritance makes that only one instance is created.
Inheritance from IContainer should be declared as virtual. Virtual inheritance makes that every derivation from a class "will get combined together" (sorry for not very technical terms but this is how I understand it in simple english). In your case, only one copy of IContainer for both paths will be created, and the vtables will get filled from both paths.
This code compiles:
#include <iostream>
#include <string>
class IContainer
{
public:
IContainer() {};
virtual ~IContainer() {};
virtual void loadDefaults() = 0;
virtual void storeDefaults() = 0;
virtual bool open() = 0;
virtual bool close() = 0;
};
class IContainerReader : virtual public IContainer
{
public:
IContainerReader() {};
virtual ~IContainerReader() {};
virtual bool read() = 0;
};
class IContainerWriter : virtual public IContainer
{
public:
IContainerWriter() {};
virtual ~IContainerWriter() {};
virtual bool write() = 0;
};
class ContainerBase : virtual public IContainer
{
public:
ContainerBase() {}
virtual ~ContainerBase() {}
void loadDefaults() {}
void storeDefaults() {}
};
class CSVBase : public ContainerBase
{
public:
CSVBase() {}
virtual ~CSVBase() {}
void setFilename() {}
bool open() { return true; }
bool close() { return true; }
};
class CSVReader : public CSVBase, public IContainerReader
{
public:
CSVReader() {}
virtual ~CSVReader() {}
bool read() { return true; }
};
class CSVWriter : public CSVBase, public IContainerWriter
{
public:
CSVWriter() {}
virtual ~CSVWriter() {}
bool write() { return true; }
};
int main(int argc, char *argv[])
{
CSVReader r;
CSVWriter w;
IContainerReader *ir = &r;
IContainerWriter *iw = &w;
ir->open();
iw->open();
ir->read();
iw->write();
ir->close();
iw->close();
return 0;
}
Related
I have two interfaces: IFace2 derives from IFace
class IFace
{
public:
virtual ~IFace() = default;
virtual void doit() = 0;
};
class IFace2 : public IFace
{
public:
virtual void doit2() = 0;
};
I am trying reduce code clutter that is required by the implementations. In IFace2Impl I would like to use the same implementation from IFaceImpl. Unfortunately, that forces me to resolve ambiguity and I need to override every function in IFace2Impl:
class IFaceImpl : public IFace
{
public:
void doit() override
{
}
};
class IFace2Impl : public IFaceImpl, IFace2
{
public:
using IFaceImpl::doit; // not resolving ambiguity
void doit2() override
{
}
};
int main()
{
IFace2Impl impl;
}
When I try to compile this code, the compiler gives me an error message:
<source>(36): error C2259: 'IFace2Impl': cannot instantiate abstract class
<source>(24): note: see declaration of 'IFace2Impl'
<source>(36): note: due to following members:
<source>(36): note: 'void IFace::doit(void)': is abstract
<source>(6): note: see declaration of 'IFace::doit'
https://godbolt.org/z/91M1j8bv3
For each virtual function I have to provide full definition:
class IFace2Impl : public IFaceImpl, IFace2
{
public:
void doit()
{
IFaceImpl::doit();
}
void doit2() override
{
}
};
Is there a way to handle this more elegantly? When number of interface method grows, the more ambiguities I have to resolve.
Virtual inheriance might help, so IFaceImpl would have only one IFace instead of 2:
class IFace
{
public:
virtual ~IFace() = default;
virtual void doit() = 0;
};
class IFace2 : public virtual IFace
{
public:
virtual void doit2() = 0;
};
class IFaceImpl : public virtual IFace
{
public:
void doit() override {}
};
class IFace2Impl :
public IFaceImpl,
public virtual IFace2 // virtual not required here, but would be with IFace3Impl...
{
public:
void doit2() override {}
};
Demo
Given a base class which has some virtual functions, can anyone think of a way to force a derived class to override exactly one of a set of virtual functions, at compile time? Or an alternative formulation of a class hierarchy that achieves the same thing?
In code:
struct Base
{
// Some imaginary syntax to indicate the following are a "pure override set"
// [
virtual void function1(int) = 0;
virtual void function2(float) = 0;
// ...
// ]
};
struct Derived1 : Base {}; // ERROR not implemented
struct Derived2 : Base { void function1(int) override; }; // OK
struct Derived3 : Base { void function2(float) override; }; // OK
struct Derived4 : Base // ERROR too many implemented
{
void function1(int) override;
void function2(float) override;
};
I'm not sure I really have an actual use case for this, but it occurred to me as I was implementing something that loosely follows this pattern and thought it was an interesting question to ponder, if nothing else.
No, but you can fake it.
Base has non-virtual float and int methods that forward to a pure virtual std variant one.
Two helper classes, one int one float, implement the std variant one, forwarding both cases to either a pure virtual int or float implementation.
It is in charge of dealing with the 'wrong type' case.
Derived inherit from one or another helper, and implement only int or float.
struct Base
{
void function1(int x) { vfunction(x); }
void function2(float x) { vfunction(x); }
virtual void vfunction(std::variant<int,float>) = 0;
};
struct Helper1:Base {
void vfunction(std::variant<int,float> v) final {
if (std::holds_alternative<int>(v))
function1_impl( std::get<int>(v) );
}
virtual void function1_impl(int x) = 0;
};
struct Helper2:Base {
void vfunction(std::variant<int,float> v) final {
if (std::holds_alternative<float>(v))
function2_impl( std::get<float>(v) );
}
virtual void function2_impl(float x) = 0;
};
struct Derived1 : Base {}; // ERROR not implemented
struct Derived2 : Helper1 { void function1_impl(int) override; }; // OK
struct Derived3 : Helper2 { void function2_impl(float) override; }; // OK
This uses https://en.wikipedia.org/wiki/Non-virtual_interface_pattern -- the interface contains non-virtual methods, whose details can be overridden to make them behave differently.
If you are afraid people will override vfunction you can use the private lock technique, and/or just give it a name like private_implementation_detail_do_not_implement and trust your code review process.
Or an alternative formulation of a class hierarchy that achieves the same thing?
One option is to have an intermediate base class that implements one function.
struct Base
{
virtual ~Base() {};
virtual void function(int) = 0;
virtual void function(float) = 0;
};
template <typename T>
struct TBase : Base
{
virtual void function(T) override {}
};
struct Derived1 : Base {};
struct Derived2 : TBase<float> { void function(int) override {} };
struct Derived3 : TBase<int> { void function(float) override {} };
int main()
{
Derived1 d1; // ERROR. Virtual functions are not implemented
Derived2 d2; // OK.
Derived3 d3; // OK.
}
Note that the functions are named function in this approach, not function1 and function2.
Your classes will remain abstract if you don't override all the abstract virtual methods. You have to do all of them if you want to instantiate the object.
I have a top API (based on abstract class) which is composed by a base API (that will be reused by different top APIs).
class api_base {
public:
virtual int foo() = 0;
};
class api_top : public api_base {
public:
virtual int bar() = 0;
}
Then, I want to provide a base implementation for base API:
class imp_base {
public:
int foo() { return 1; }
}
And finally implement top API using base implementation for those functions defined in base API:
class imp_top : public api_top, public imp_base {
public:
int bar() { return 1; }
}
When I instantiate an object of imp_top type, compiler say that foo() function is not implemented. But that is not true, since imp_top derives from imp_base, which do have foo() function implemented.
Any advice about this?
Thank you in advance.
Full test code:
#include <stdio.h>
class api_base {
public:
virtual int foo() = 0;
};
class api_top : public api_base {
public:
virtual int bar() = 0;
};
class imp_base {
public:
int foo() { printf("foo from imp_base\n"); }
};
class imp_top : public api_top, public imp_base {
public:
int bar() { printf("bar from imp_top\n"); }
};
int main()
{
printf("Hello\n");
imp_top a;
a.bar();
a.foo();
return 1;
}
Compiler result:
test.cpp:26:12: error: cannot declare variable ‘a’ to be of abstract type ‘imp_top’
imp_top a;
^
test.cpp:18:7: note: because the following virtual functions are pure within ‘imp_top’:
class imp_top : public api_top, public imp_base {
^
test.cpp:5:17: note: virtual int api_base::foo()
virtual int foo() = 0;
^
test.cpp:29:6: error: request for member ‘foo’ is ambiguous
a.foo();
^
test.cpp:15:9: note: candidates are: int imp_base::foo()
int foo() { printf("foo from imp_base\n"); }
^
test.cpp:5:17: note: virtual int api_base::foo()
virtual int foo() = 0;
First Always use override keyword when redefining the functions in child classes.
Second read about virtual inheritance.
The following works :
class api_base {
public:
virtual int foo() = 0;
};
class api_top : public virtual api_base {
public:
virtual int bar() = 0;
};
class imp_base : public virtual api_base {
public:
int foo() override { printf("foo from imp_base\n"); return 0; }
};
class imp_top : public api_top, public imp_base {
public:
int bar() override { printf("bar from imp_top\n"); return 0; }
};
int main(){
imp_top a;
a.bar();
a.foo();
return 1;
}
Change:
class api_top : public api_base to class api_top : public virtual api_base
and:
class imp_base to class imp_base : public virtual api_base
Then it works.
To understand this, see: virtual inheritance. And yes (just saw Ext3h's post), use the override keyword.
You should have used the override keyword, then you would had noticed that you did not implement the interface but defined an entirely new foo() method.
You will need to derive both imp_base and api_top from api_base by virtual inheritance.
I do have some general data type and some derived type(s):
class abstract_data { virtual void foo() {}; };
class derived_data : public abstract_data { void foo() {} };
I want to write some classes that work on that data. What I would like to do is:
class abstract_worker {
public:
virtual void apply(abstract_data&) = 0;
};
class derived_worker : public abstract_worker {
public:
void apply(derived_data&) {} ;
};
This is not working, because the compiler asks me to implement void apply(abstract_data&). I see two alternatives.
1.alternative:
class abstract_worker {
public:
virtual void apply(abstract_data&) = 0;
};
class derived_worker : public abstract_worker {
public:
void apply(abstract_data& data) {
derived_data& internal_data = dynamic_cast<derived_data&>(data);
}
};
2. alternative:
template<class DATA_TYPE>
class abstract_worker {
public:
virtual void apply(DATA_TYPE&) = 0;
};
class derived_worker : public abstract_worker<derived_data> {
public:
void apply(derived_data&) { }
};
I don't like both of them. In the 1st alternative the parameter is declared as abstract_data& even though it really needs to be derived_data&.
In the 2nd alternative it gets lost that the parameter is of type abstract_data&. This is even more problematic if apply(abstract_data&) is not pure virtual, but uses some functionality of abstract_data. You run in the situtation where abstract_data& is implicitely required as DATA_TYPE, but not explicitely declared as such.
However, I do want to keep the type relationships.
Is there another way? If not which one would you prefer?
You might use static assert to prevent DATA_TYPE not inheriting from abstract_data:
#include
class abstract_data { virtual void foo() {}; };
class derived_data : public abstract_data { void foo() {} };
class faulty_data { void foo() {} };
template<class DATA_TYPE>
class abstract_worker {
static_assert(std::is_base_of<abstract_data, DATA_TYPE>::value, "DATA_TYPE does not inherit from abstract_data");
public:
virtual void apply(DATA_TYPE&) = 0;
};
class derived_worker : public abstract_worker<derived_data> {
public:
void apply(derived_data&) {}
};
class faulty_worker : public abstract_worker<faulty_data> {
public:
void apply(faulty_data&) {}
};
Output while compiling:
1>------ Build started: Project: Test_Call, Configuration: Debug Win32 ------
1> Source.cpp
1><PROJ_PATH>\source.cpp(9): error C2338: DATA_TYPE does not inherit from abstract_data
1> <PROJ_PATH>\source.cpp(20): note: see reference to class template instantiation 'abstract_worker<faulty_data>' being compiled
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
There are two base classes have same function name. I want to inherit both of them, and over ride each method differently. How can I do that with separate declaration and definition (instead of defining in the class definition)?
#include <cstdio>
class Interface1{
public:
virtual void Name() = 0;
};
class Interface2
{
public:
virtual void Name() = 0;
};
class RealClass: public Interface1, public Interface2
{
public:
virtual void Interface1::Name()
{
printf("Interface1 OK?\n");
}
virtual void Interface2::Name()
{
printf("Interface2 OK?\n");
}
};
int main()
{
Interface1 *p = new RealClass();
p->Name();
Interface2 *q = reinterpret_cast<RealClass*>(p);
q->Name();
}
I failed to move the definition out in VC8. I found the Microsoft Specific Keyword __interface can do this job successfully, code below:
#include <cstdio>
__interface Interface1{
virtual void Name() = 0;
};
__interface Interface2
{
virtual void Name() = 0;
};
class RealClass: public Interface1,
public Interface2
{
public:
virtual void Interface1::Name();
virtual void Interface2::Name();
};
void RealClass::Interface1::Name()
{
printf("Interface1 OK?\n");
}
void RealClass::Interface2::Name()
{
printf("Interface2 OK?\n");
}
int main()
{
Interface1 *p = new RealClass();
p->Name();
Interface2 *q = reinterpret_cast<RealClass*>(p);
q->Name();
}
but is there another way to do this something more general that will work in other compilers?
This problem doesn't come up very often. The solution I'm familiar with was designed by Doug McIlroy and appears in Bjarne Stroustrup's books (presented in both Design & Evolution of C++ section 12.8 and The C++ Programming Language section 25.6). According to the discussion in Design & Evolution, there was a proposal to handle this specific case elegantly, but it was rejected because "such name clashes were unlikely to become common enough to warrant a separate language feature," and "not likely to become everyday work for novices."
Not only do you need to call Name() through pointers to base classes, you need a way to say which Name() you want when operating on the derived class. The solution adds some indirection:
class Interface1{
public:
virtual void Name() = 0;
};
class Interface2{
public:
virtual void Name() = 0;
};
class Interface1_helper : public Interface1{
public:
virtual void I1_Name() = 0;
void Name() override
{
I1_Name();
}
};
class Interface2_helper : public Interface2{
public:
virtual void I2_Name() = 0;
void Name() override
{
I2_Name();
}
};
class RealClass: public Interface1_helper, public Interface2_helper{
public:
void I1_Name() override
{
printf("Interface1 OK?\n");
}
void I2_Name() override
{
printf("Interface2 OK?\n");
}
};
int main()
{
RealClass rc;
Interface1* i1 = &rc;
Interface2* i2 = &rc;
i1->Name();
i2->Name();
rc.I1_Name();
rc.I2_Name();
}
Not pretty, but the decision was it's not needed often.
You cannot override them separately, you must override both at once:
struct Interface1 {
virtual void Name() = 0;
};
struct Interface2 {
virtual void Name() = 0;
};
struct RealClass : Interface1, Interface2 {
virtual void Name();
};
// and move it out of the class definition just like any other method:
void RealClass::Name() {
printf("Interface1 OK?\n");
printf("Interface2 OK?\n");
}
You can simulate individual overriding with intermediate base classes:
struct RealClass1 : Interface1 {
virtual void Name() {
printf("Interface1 OK?\n");
}
};
struct RealClass2 : Interface2 {
virtual void Name() {
printf("Interface2 OK?\n");
}
};
struct RealClass : RealClass1, RealClass2 {
virtual void Name() {
// you must still decide what to do here, which is likely calling both:
RealClass1::Name();
RealClass2::Name();
// or doing something else entirely
// but note: this is the function which will be called in all cases
// of *virtual dispatch* (for instances of this class), as it is the
// final overrider, the above separate definition is merely
// code-organization convenience
}
};
Additionally, you're using reinterpret_cast incorrectly, you should have:
int main() {
RealClass rc; // no need for dynamic allocation in this example
Interface1& one = rc;
one.Name();
Interface2& two = dynamic_cast<Interface2&>(one);
two.Name();
return 0;
}
And here's a rewrite with CRTP that might be what you want (or not):
template<class Derived>
struct RealClass1 : Interface1 {
#define self (*static_cast<Derived*>(this))
virtual void Name() {
printf("Interface1 for %s\n", self.name.c_str());
}
#undef self
};
template<class Derived>
struct RealClass2 : Interface2 {
#define self (*static_cast<Derived*>(this))
virtual void Name() {
printf("Interface2 for %s\n", self.name.c_str());
}
#undef self
};
struct RealClass : RealClass1<RealClass>, RealClass2<RealClass> {
std::string name;
RealClass() : name("real code would have members you need to access") {}
};
But note that here you cannot call Name on a RealClass now (with virtual dispatch, e.g. rc.Name()), you must first select a base. The self macro is an easy way to clean up CRTP casts (usually member access is much more common in the CRTP base), but it can be improved. There's a brief discussion of virtual dispatch in one of my other answers, but surely a better one around if someone has a link.
I've had to do something like this in the past, though in my case I needed to inherit from one interface twice and be able to differentiate between calls made on each of them, I used a template shim to help me...
Something like this:
template<class id>
class InterfaceHelper : public MyInterface
{
public :
virtual void Name()
{
Name(id);
}
virtual void Name(
const size_t id) = 0;
}
You then derive from InterfaceHelper twice rather than from MyInterface twice and you specify a different id for each base class. You can then hand out two interfaces independently by casting to the correct InterfaceHelper.
You could do something slightly more complex;
class InterfaceHelperBase
{
public :
virtual void Name(
const size_t id) = 0;
}
class InterfaceHelper1 : public MyInterface, protected InterfaceHelperBase
{
public :
using InterfaceHelperBase::Name;
virtual void Name()
{
Name(1);
}
}
class InterfaceHelper2 : public MyInterface, protected InterfaceHelperBase
{
public :
using InterfaceHelperBase::Name;
virtual void Name()
{
Name(2);
}
}
class MyClass : public InterfaceHelper1, public InterfaceHelper2
{
public :
virtual void Name(
const size_t id)
{
if (id == 1)
{
printf("Interface 1 OK?");
}
else if (id == 2)
{
printf("Interface 2 OK?");
}
}
}
Note that the above hasn't seen a compiler...
class BaseX
{
public:
virtual void fun()
{
cout << "BaseX::fun\n";
}
};
class BaseY
{
public:
virtual void fun()
{
cout << "BaseY::fun\n";
}
};
class DerivedX : protected BaseX
{
public:
virtual void funX()
{
BaseX::fun();
}
};
class DerivedY : protected BaseY
{
public:
virtual void funY()
{
BaseY::fun();
}
};
class DerivedXY : public DerivedX, public DerivedY
{
};
There are two other related questions asking nearly (but not completely) identical things:
Picking from inherited shared method names. If you want to have rc.name() call ic1->name() or ic2->name().
Overriding shared method names from (templated) base classes. This has simpler syntax and less code that your accepted solution, but does not allow for access to the functions from the derived class. More or less, unless you need to be able to call name_i1() from an rc, you don't need to use things like InterfaceHelper.