I have a vector of pointers to derived objects insert by the user (so I guess the correct term is “known only in runtime)::
vector<Person *> vect;
The derived classes are Male and Female.
I want to make an iteration on the vector to select only the Female objects and call the copyconstructor of that.
I thought 3 solutions:
- To use a flag;
- To use typeid
- To insert a calling to the copy constructor in the default constructor of Female so every time the user creates one, automatically create the twin.
I don’t like the first option in the case of many kind of derived classes.
I don’t like the third option too because would cause a problem of relationship (the World knows every Female but the Female can’t know the World).
So I should use the second option:
example
typeid(vect.at(i))==typeid(Female)
Is this expression correct?
Is there another way to outline the problem?
Having
MaleandFemaleinherit fromPersonsounds like a really strange design, but here we go:If you really want to perform a copy of the female, which again sounds strange, replace the last line with: