Sorry to surprise you:
None of the examples you cited should be considered as the βright wayβ to handle class sets in C ++.
Also, if possible, don't mention std :: array or std :: vector, I'm still too new
No, this is the wrong way to raise a mare. The proper use of source pointers and raw arrays certainly goes beyond your capabilities if you cannot figure out how to handle std::array or std::vector in the first place.
Suppose your Employee class looks like
struct Employee { std::string surname_; std::string firstname_; enum Gender { Female = 'F' , Male = 'M' , Unxpecified = 'X' } gender_; };
and you have overload for std::operator>>()
std::istream& operator>>(std::istream& is, Employee& employee) { char genderSymbol; is >> employee.surname_ >> employee.firstname_ >> genderSymbol; switch(genderSymbol) { case 'F': case 'M': case 'X': employee.gender_ = (Employee::Gender)genderSymbol; break; default: is.setstate(std::ios_base::failbit); break; } }
One good and idiomatic way to represent an Employee array is to use
std::vector<Employee> employeeArr;
and fill it in the loop:
Employee employee; while(std::cin >> employee) { employeeArr.emplace_back(employee); }
If you really need pointers (links), you can use smart pointers, as provided in the Dynamic Memory Management utility .
For example, you can choose
std::vector<std::unique_ptr<Employee>> employeeArr;
and initialize it as
while(std::cin >> surname >> firstname >> gender) { employeeArr.emplace_back(std::make_unique<Employee>(surname , firstname , gender)); }
This is taken into account if you want to manage pools of hierarchically organized instances of a class, for example:
struct Employee { virtual ~Employee() {} std::string surname_; std::string firstname_; enum Gender { Female = 'F' , Male = 'M' , Unxpecified = 'X' } gender_; }; struct IForeman : Employee { virtual std::vector<const Employee const*> TeamMembers() const = 0; virtual void AddTeamMember(const Employee const* member) = 0; }; class Foreman : public IForeman { str::vector<const Employee const*> teamMembers_; public: std::vector<const Employee const*> TeamMembers() const { return teamMembers_; } void AddTeamMember(const Employee const* member) { teamMembers_.push_back(member); } };
Consider passing in owned or shared pointers to related connections using simple const pointers.