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;
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.