I want to recombine a (complex) member attribute of two agents and put it in a new agent. It's a vector of numbers, every second value is taken from agent1, the rest from agent2. The problem is, I want to be able to exchange the implementation of my numberList, maybe another numberListInt2 using integers or like in my example using floats:
#include <vector>
using namespace std;
class NumberList {
};
class NumberListInt : public NumberList {
vector<int> number_list {1,2,3};
};
class NumberListFloat : public NumberList {
vector<float> number_list {1.2f,2.5f,30.0f};
};
class Agent {
NumberList* numbers;
public:
Agent();
Agent(NumberList* numbers) {
numbers = numberList*
}
~Agent() {
delete numbers;
}
NumberList* recombine(Agent& other) {
NumberList* new_number_list;
if(true) // a boolean config value
new_number_list = new NumberListInt();
else
new_number_list = new NumberListFloat();
for(unsigned int i=0;i<3;i++) {
if(i%2)
new_number_list[i] = other.get_number_list()[i];
else
new_number_list[i] = numbers[i];
}
return new_number_list;
}
NumberList* get_number_list() {
return numbers;
}
};
int main ()
{
Agent agent;
Agent agent2;
Agent agent3(agent.recombine(agent2));
return 0;
}
My questions:
- How to implement the operator
[]ofNumberList? - Is there a better way than using a pointer for the polymorphism?
- Do I free the memory correctly?
Thanks in advice!
Aucun commentaire:
Enregistrer un commentaire