I have this code:
class Foo
{
public:
int x = 4;
int & operator[](size_t index) { return x; }
};
class Bar : protected Foo
{
public:
using Foo::operator[];
Bar () { x++; }
};
int main(int agrc, char ** argv)
{
typedef int &(Bar::*getOp)(size_t index);
Bar b;
auto bVal = b[4];
getOp o = &Bar::operator[];
auto bVal2 = (b.*o)(7);
}
However, I cannot compile this, because
error C2247: 'Foo' not accessible because 'Bar' uses 'protected' to inherit from 'Foo'
Why is this not possible, when I have used using
and I can call operator directly? Is there any way around?
If I change inheritance to public, it is working.
Note: This is just an example of larger class. I dont want to use public inheritance, because I dont want to be able to do Foo f = Bar()
because in Bar
, I am hiding parent methods (I have not use virtual).
(b.*o)(7);
or(bVal[some_valid_index].*o)(7);
– NathanOliverop
is defined? – João Paulo