Answer the question
In order to leave comments, you need to log in
Is it possible to use protected class constructors in an inheritor?
Good afternoon.
I am reading Stroustrup's book and there, using the example of working with the FLTK graphics library, methods of inheritance are explained. I got to one moment, the approximate code of which is given below:
class A {
protected:
A();
A(initializer_list<int> lst) {
for (auto a : lst)
add(a);
};
void add(int a) {
vals.push_back(a);
}
public:
~A() {};
void get_vals(void) const {
for (auto i : vals)
cout << i << '\n';
}
private:
vector<int> vals;
};
struct B : A {
using A::A;
};
B elem{1, 2, 3, 4};
error: 'B::B(std::initializer_list<int>)' is protected
IntelliSense: no instance of constructor "B::B" matches the argument list argument types are: (int, int, int, int) ProtectedTest
Answer the question
In order to leave comments, you need to log in
Apparently, this error can be corrected only by explicitly declaring the necessary constructor in the inherited class, using the base constructor:
struct B : A {
B(initializer_list<int> lst) : A{lst} {}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question