c++ - is this possible to inherit private class but make members public? -
i want have members inherited private. think saw example of making them public nevertheless fact derived private keyword. question: how it, , if possible shouldn't prohibited?
class u{ public: int a; protected: int b; private: int c; }; class v : private u{ public: int i; //can make public again? };
how it?
perfectly possible, use using
keyword.
shouldn't prohibited?
no need. can return members back accessibility not more initially. so, if base class has declared public , idea/restriction make private, doesn't hurt base class if drop restriction , leave public, in public @ beginning. cite "c++ programming language" best here.
a using-declaration cannot used gain access additional information. mecha- nism making accessible information more convenient use.
so if accessible in base class, , derived class protected
or private
keyword can remove restriction , return them initial level of access "transporting" them appropriate part (public,protected,private
) in derived class definition.
class u{ public: int a; protected: int b; private: int c; }; class v : private u{ public: using u::b; using u::a; }; int main(int argc, char** argv) { v v; printf("\nv: %d %d %d",v.a,v.a,v.b); u u; printf("\nu: %d %d %d",u.a,u.a,u.a); return 0; }
Comments
Post a Comment