Derived class member function not being called

Pages: 12
Why does your set current area method take a player when that argument is never even used?
do I need to clear the current object being pointed to from the player's memory somehow, i.e with a delete function? Not sure if simply redefining the pointer in the player class is enough, but had a lot of compilation errors trying to add deletes in, still quite new to cpp and programming in general (this is my first major project) so apologies for any potentially dumb qns.


If the pointer inside of player is dynamically allocated, then you need to delete the dynamic memory before you change the pointer or else you have a memory leak. If it is not dynamically allocated, then you do not need to call delete on it.
Last edited on
I've worked out what's causing the problem despite not knowing exactly why, it was to do with the creation and initialisation of a player with a test village in game.cpp, for some reason despite showing the correct outputs for the village outskirts in the inputoutput function, the program still remembers the player was initialised with a village object, so reverts to that when calling the function in question. I'll work on the player constructor and mark this thread as solved if I resolve my issue. Many thanks to everyone for the help of course
player::set_current_area() is incorrect. It's blindly assigning an area to another. Doing this with base classes is extremely error prone. Besides, the player doesn't own the object pointed to by Player::current_area, so the behavior is unpredictable.
I suspect what you intended to do was
1
2
3
void player::set_current_area(area &a) {
	current_area = &a;
}

As I said the first time, the type of the object when inputoutputhandler::interact_with_area() is called is village. It's just that its members have been reassigned. The description says "This is the introduction to the village outskirts" and the type says "village outskirts".
Last edited on
Oh man thank you so much!! I never would have got this otherwise
Topic archived. No new replies allowed.
Pages: 12