I have a class Goblin
, which inherits from Enemy
.
I'm attempting to run a for
loop to set the name of all of the goblins. But the loop isn't settings the names, and I get the default "Enemy".
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Enemy {
public:
string getName() {return name; }
void setName(string _name) { name = _name; }
private:
string name = "Enemy";
};
class Goblin : public Enemy {};
int main(int argc, char **argv)
{
Goblin g1,g2;
std::vector<Goblin> goblins={g1,g2};
for (Goblin x : goblins)
{
x.setName("test_name");
}
std::cout << goblins[0].getName();
return 0;
}