std::basic_ios
has a public constructor:
explicit basic_ios (std::basic_streambuf<CharT,Traits>* sb);
IMO, the sole reason for a class to have a public constructor is to use a standalone instance of that class in a program. If a class exists only to have other classes descend from it (as seems to be the case for basic_ios
), all of the class's constructors should be protected
. The constructors of std::ios_base
are all protected. But, for some reason, the designers of the standard made this one constructor of basic_ios
public.
basic_ios
is used as a base class for several stream types, and I can't envision a use case where you'd have one that wasn't at least a basic_istream
or basic_ostream
. Is there one?
The other reason for a class to have a public constructor is to have this constructor signature available to construct a derived object:
struct B{
B(int);
protected:
~B();
};
struct A:B{
private://no effect.
using B::B;
public:
A(void*);
};
A a(10);
The constructor must be public in the base class because a using declaration of a base constructor does not change the accessibility of the inherited constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With