Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7878059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:33:09+00:00 2026-06-03T03:33:09+00:00

I am using a boost::ptr_vector , but I believe this applies to a standard

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:33:10+00:00Added an answer on June 3, 2026 at 3:33 am

    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).

    #include "boost/ptr_container/ptr_vector.hpp"
    #include <iostream>
    #include <string>
    #include <sstream>
    
    class Object { 
        std::string name;
    public:
        Object(std::string const &n) : name(n) {}
    
        virtual std::ostream &write(std::ostream &os) const {
            return os << name;
        }
    
        virtual ~Object() { std::cout << "Object being destroyed\n"; }
    };
    
    class Entity : public Object { 
        int value;
    public:
        Entity(int v, std::string name) : Object(name), value(v) {}
    
        std::ostream &write(std::ostream &os) const { 
            return os << "Entity: " << value;
        }
        ~Entity() { std::cout << "Entity being destroyed\n"; }  
    };
    
    int main() { 
        boost::ptr_vector<Object> objects;
    
        for (int i=0; i<10; i++) {
            std::stringstream name;
            name << "object: " << i;
            if (i & 1)
                objects.push_back(new Object(name.str()));
            else
                objects.push_back(new Entity(i, name.str()));
        }
    
        boost::ptr_vector<Object>::iterator pos;
        for (pos = objects.begin(); pos != objects.end(); pos++) {
            pos->write(std::cout);
            std::cout << "\n";
        }
        return 0;
    }
    

    At least for me, the output looks like this:

    Entity: 0
    object: 1
    Entity: 2
    object: 3
    Entity: 4
    object: 5
    Entity: 6
    object: 7
    Entity: 8
    object: 9
    Entity being destroyed
    Object being destroyed
    Object being destroyed
    Entity being destroyed
    Object being destroyed
    Object being destroyed
    Entity being destroyed
    Object being destroyed
    Object being destroyed
    Entity being destroyed
    Object being destroyed
    Object being destroyed
    Entity being destroyed
    Object being destroyed
    Object being destroyed
    

    For what it’s worth, note the destructors — when an Object is destroyed, only the base dtor is invoked, but when an Entity is destroyed, first the derived dtor is invoked, then the base dtor is invoked (so we see both ‘Entity being destroyed’ and ‘Object being destroyed”).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a container of pointers. Would you recommend boost::ptr_vector<T> or std::vector<boost::shared_ptr<T> > ?
I'm trying to add a boost::ptr_vector to a std::deque, using push_back(). When I do,
#include <boost/ptr_container/ptr_vector.hpp> #include <iostream> using namespace std; using namespace boost; struct A { ~A()
Usually like this: #include <boost/assign/std/vector.hpp> vector<int> v; v += 1,2,3,4,5; Except for a: #include
I need to have a std::vector of boost::ptr_vector s. To make their management easier,
How would you serialize/deserialize this class using boost::serialization? #include <vector> struct Foo { struct
For my current project I am using a boost::ptr_vector to hold Objects polymorphically, and
#include <boost/ptr_container/ptr_vector.hpp> #include <iostream> using namespace std; class Derived { public: int i; Derived()
How much do using smart pointers, particularly boost::shared_ptr cost more compared to bare pointers
What benefits has using std::reference_wrapper as template parameter of containers instead of raw pointers?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.