I am using a boost::ptr_vector, but I believe this applies to a standard std::vector as well. I am trying to place pointers to objects polymophically into a boost::ptr_vector the hierarchy is that I have an Entity that inherits from Object being created with the line
Object * newObject = new Entity(param1, param2); // then I attempt to add it to the ptr_vector
but if I break the program (Visual Studio 2010) to look at what is being held the pointer is never redirected from garbage, and garbage is being held. I step through the code, and it does enter the parameterized constructor, and follows the correct logical steps with it.
I am uncertain what is going wrong. do I need to have specific member functions in the parent, or the child in order for this polymorphic behavior to work (currently all children have parameterized constructors unique to their type, and destructors along with polymorphic interaction methods). must I have assignment operators, or should I have a constructor in the Object class.
It seems to be that the call to operator new is not resolving to an object, but resolving to something else, but VS2010 is not throwing an error.
Edit: explanation of what should be happening.
stepping through a 2D std::vector(rectangular/non-jagged)
using case/switch to determine object to be generated, and added to structure
pointer to Object is created, and assigned to new // I think this is where the problem is happening
then the reference of that pointer is pushed onto a manager-member boost::ptr_vector
in Visual Studio 2010 I put a break at the line to create the pointer, and assign new (polymorphic), and one on the line for the push_back() to the boost::ptr_vector watching the pointer. The temp pointer value is created, and stepping into the constructor it follows all logical steps for that constructor, and when the constructor finishes, and the stack returns to the line that called the constructor the pointer is still the same value (I think this is acceptable), but when I look at the object that it points to all the values show up as question marks (including the statically composed member objects). then when the push back triggers, and enters boost-header the x value show the same information.
it almost seems like the pointer is being made, and the datams of the object are created, but once the constructor is finished it doesn’t actually assign the values to the parent class object which should be considerably simple with regards to polymophic behavior.
example headers of concern (the real headers do have member variables, and their implementations are in a separate cpp file):
class Object{
public :
virtual void interact(int action, Object& source){}
virtual void updateObject(float duration){}
virtual ~Object(){}
bool operator==(const Object& _other)const;
bool operator!=(const Object& _other)const;
};
class Entity : public Object{
public:
Entity(Vector3 location, Type thisType, SpecialType difficulty=noSpecial);
~Entity();
void interact(int action, Object& source);
void updateObject(float duration);
};
Edit: changing context to better target problem at hand, and receive a solution
Based on what you’ve posted, it’s difficult (if even possible) to be sure of the problem, not to mention how it’s coming about/how to fix it.
Perhaps it’s better to start from something that actually works, and add the functionality you need, or at least get some idea of places you’re deviating from what’s expected/what works. So, here’s a small sample of creating objects dynamically, putting them into a
ptr_vector, using a virtual function in each to verify that what’s in the container is what’s expected, and then letting the container go out of scope (and in the process, destroying the objects referred to by the pointers it contains).At least for me, the output looks like this:
For what it’s worth, note the destructors — when an
Objectis destroyed, only the base dtor is invoked, but when anEntityis destroyed, first the derived dtor is invoked, then the base dtor is invoked (so we see both ‘Entity being destroyed’ and ‘Object being destroyed”).