Given
#include <iostream>
#include <string>
using std::cout;
using std::endl;
class A
{
public:
virtual int a() const = 0;
virtual int a(const int i) const = 0;
};
class B : public A
{
public:
virtual int a() const { return A::a(0); }
};
class C : public B
{
public:
virtual int a(const int i) const { return i; }
};
int main()
{
C c;
cout << c.a() << endl;
cout << c.a(1) << endl;
}
I did not tests with different compilers but i have the following errors:
main.cpp:31:17: error: no matching function for call to ‘C::a()’
cout << c.a() << endl;
^
main.cpp:24:17: note: candidate: virtual int C::a(int) const
virtual int a(const int i) const { return i; }
^
main.cpp:24:17: note: candidate expects 1 argument, 0 provided
I do not understand really why, I was expecting that class C
can access at B::a()
.
It seems that is resolving the Ca()
as Ca(int)
. Basically the virtual method from B
is not visible. Anyone knows why?
EDIT
The idea is something like: A
as "interface", B
as "abstract class", C
the "concrete class"
SOLUTION
What i was trying to achieve is possible with using A::a
using B::a
and changing the B::a()
call from A::a(0)
to a(0)
.
Like the example here provided by songyuanyao
C::a
hides B::a
. When the name a
is found at the scope of class C
, name lookup stops, further scopes including class B
won't be checked.
name lookup examines the scopes as described below, until it finds at least one declaration of any kind, at which time the lookup stops and no further scopes are examined.
You can add using
to introduce B::a
into C
's scope.
class C : public B
{
public:
using B::a;
virtual int a(const int i) const { return i; }
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.