Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `std::basic_ios` have a public constructor?

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?

like image 850
Spencer Avatar asked Nov 26 '19 16:11

Spencer


1 Answers

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.

like image 168
Oliv Avatar answered Nov 12 '22 16:11

Oliv