Thank you very much! I will rethink the style (still very new to cpp, and it shows). Over the past few months, I have learnt so much from all of you in this forum and I greatly appreacite it.
I have a follow up question (sorry, still in the old style):
I need to instantiate a player by calling a function that returns a typed (shared) pointer, which can either be typed KnightPtr or CowboyPtr.
The pointer goes out of scope when leaving the if-statement. How can I preserve the pointer by defining it outside of the if-statment if I don't know which type will come out of the if-statement?
I guess I need some generic pointer to by cast inside the if-statement. How best to do this?
Code:
if (A_type == "knight") {
KnightPtr A = ConstructKnight("\nKnight A", A_loadout);
} else {
CowboyPtr A = ConstructCowboy("\nCowboy A", A_loadout);
}
PrintPlayer(A);
For example somethink like:
Code:
PlayerPtr A = nullptr;
if (A_type == "knight") {
// cast A to KnightPtr
A = ConstructKnight("\nKnight A", A_loadout);
} else {
// cast A to CowboyPtr
A = ConstructCowboy("\nCowboy A", A_loadout);
}
PrintPlayer(A);
More info: classes Knight and Cowboy inherid from Player, but are not polymorphic to it (class Player is not vitrual). The function PrintPlayer (despite its name) cannot be applied to Player but only to Knight or Cowboy, because Player does not hold all the data.