Sign in to follow this  
bencelot

Making copies of virtual objects

Recommended Posts

Hey all, I have a 'Mission' class, and a number of specific missions that extend from this base class. For example: class Mission { public: int missionLevel, cashReward; bool completed, failed; Mission(); virtual void Update() {cout << "base" << endl;} }; class Cop1 : public Mission { public: int arrestsRequired; Cop1(); void Update() {cout << "mission1" << endl;} }; etc.. I then have a Mission Manager object, which holds a vector of pointers to these missions. When a player selects a new mission, I want them to make their own copy of one of these missions, and update it as they kill/arrest people, etc. The problem I'm having is that when I make a copy of one of these missions, only a copy of the base class is made, not the specific mission. For example: Cop1 mission1; std::vector<Mission*> copMissions; copMissions.push_back(&mission1); Mission tempMission = *copMissions[0]; tempMission.Update(); //OUTPUTS: "base" I would hope that this would output "mission1", but it doesn't.. it outputs "base". I think I know why this is happening.. because I'm casting tempMission as Mission... However, if I do this: Cop1 mission1; std::vector<Mission*> copMissions; copMissions.push_back(&mission1); Mission* tempMission = copMissions[0]; tempMission->Update(); //OUTPUTS: "mission1" It correctly outputs "mission1".. but this time it doesn't make a copy of the mission.. but rather is alterting the original mission.. which I don't want. Basically I have to combine the two.. maybe I can use the heap? But I don't know how to create the right type.. I'd like to do something like: Mission* tempMission = new copMissions[0]; that doesn't work, obviously.. but hopefully it shows what I'm trying to achieve. Any ideas? Thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this