You are returning a pointer to a local array that goes out of scope and gets destroyed when the function exits, thus destroying the objects in the array. The caller ends up receiving a dangling pointer to invalid memory, and accessing the elements via that pointer is undefined behavior.
Consider returning a std::vector<CTransponder>
or std::array<CTransponder, 10>
instead, eg:
std::vector<CTransponder> CVerwaltung::Initial(){
return std:vector<CTransponder>{
CTransponder("Chef", 100),
CTransponder("Chefin", 101),
...
};
}
Otherwise, make the array be a member of CVerwaltung
, eg:
class CVerwaltung{
private:
CTransponder Transpo[10];
public:
CVerwaltung();
CTransponder* Initial();
};
CVerwaltung::CVerwaltung() {
Transpo[0] = CTransponder("Chef", 100);
Transpo[1] = CTransponder("Chefin", 101);
...
}
CTransponder* CVerwaltung::Initial() {
return Transpo;
}
Or, just move the array into global scope, eg:
CTransponder Transpo[10] = {
CTransponder("Chef", 100),
CTransponder("Chefin", 101),
...
};
std::vector<CTransponder> CVerwaltung::Initial(){
return Transpo;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…