1

I would like to use the base class constructor in a subclass, the base class constructor is protected so it cannot be instantiated without a subclass.

class A {
protected:
    A() {}
};

class B : public A {
public:
    B() : A() {}
};

but I cannot use a using directive like this without the compiler complaining that B::B() is protected, even though the using is in the public block.

class B {
public:
    using A::A;
};

Where is this behavior specified?

EDIT

I attempted to simplify the example, and did not check to see it compiles, apparently with a trivial constructor it works, but this code does not compile

class A {
protected:
    A(int x) {}
};

class B : public A{
public:
    using A::A;
};

int main() {
  B b(4);
}

with error

main.cpp: In function 'int main()':
main.cpp:12:8: error: 'A::A(int)' is protected within this context
   B b(4);
        ^
main.cpp:3:5: note: declared protected here
     A(int x) {}
     ^

exit status 1
shane
  • 1,742
  • 2
  • 19
  • 36

0 Answers0