I am trying to test protected methods and constructors of my class. For this purpose, I tried to subclass it, and re-export its members as public with C++11 using
keyword:
class Foo {
protected:
Foo(int i) {}
void run() {}
};
class TestableFoo : public Foo {
public:
using Foo::Foo;
using Foo::run;
};
int main() {
TestableFoo foo(7);
foo.run();
}
However, both g++ and clang++ fail to compile it, producing the following error:
test.cpp:13:15: error: ‘TestableFoo::TestableFoo(int)’ is protected
using Foo::Foo;
^
test.cpp:18:16: error: within this context
TestableFoo foo(7);
^
TestableFoo constructor is still protected, even though run
method becomes public (I confirmed it separately). Why is that so? I could understand either decision (inheriting vs. overwriting visibility), but why is there an inconsistency between methods and constructors?