In C++, I can access private variables of a struct, but not the private variables within the sub-struct -
struct teachers { private: int graddate; int quote; string name; string school; struct smallboard { private: vector<string> grade; vector<string> school; vector<string> emblem; vector<int> year; }; smallboard sb; public: static void retrieveinformation(vector<teachers> &_teachers); static void t_addinfo(vector<teachers> &_teachers, teachers teachers_); static void s_addinfo(vector<teachers::smallboard> &_smallboard, teachers::smallboard smallboard_); };
i can say
teachers tempt; tempt.name = "bob";
but whenever attempt access variable within smallboard struct tells me private. i'm assuming method of accessing smallboard incorrect, how accomplish that?
declaring smallboard
nested type inside of teachers
not change fact smallboard
still own data type own access rules. in order teachers
access private members of smallboard
, teachers
must declared friend
of smallboard
.
struct smallboard { private: vector<string> grade; vector<string> school; vector<string> emblem; vector<int> year; friend struct teachers; };
also, if smallboard
declared private
type of teachers
cannot used in parameters of public methods of teachers
, since callers never able access or instantiate smallboard
objects. have make smallboard
public:
struct teachers { private: int graddate; int quote; string name; string school; smallboard sb; public: struct smallboard { private: vector<string> grade; vector<string> school; vector<string> emblem; vector<int> year; friend struct teachers; }; static void retrieveinformation(vector<teachers> &_teachers); static void t_addinfo(vector<teachers> &_teachers, teachers teachers_); static void s_addinfo(vector<teachers::smallboard> &_smallboard, teachers::smallboard smallboard_); };
Comments
Post a Comment