Sorry to surprise you:
None of the examples you give should be considered as as the "correct way" to handle collections of classes in c++.
Also if possible, do not mention std::array or std::vector, I'm still too new
No, that's the wrong path hauling up the mare. The proper usage of raw pointers and raw arrays is certainly beyond your capabilities, if you can't grasp how to deal with std::array
or std::vector
primarily.
Supposed 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 an overload for the 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 that Employee
array would be to use a
std::vector<Employee> employeeArr;
and fill it in a loop:
Employee employee;
while(std::cin >> employee) {
employeeArr.emplace_back(employee);
}
If you really need pointers (references) you may consider to use smart pointers as provided with the Dynamic Memory Management utility classes.
For instance you may decide to have a
std::vector<std::unique_ptr<Employee>> employeeArr;
and initialize it like
while(std::cin >> surname >> firstname >> gender) {
employeeArr.emplace_back(std::make_unique<Employee>(surname , firstname , gender));
}
This comes into consideration if you want to manage pools of hierarchically organized class instances like:
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 to hand out owned or shared pointers to related connections using plain const
pointers.