   Tree using pointers c++
  I have a tree type structure for a node in my scene graph.It could be defined as such:class Node{  /* ... */  std::vector <Node*> children;  /* ... */};And then there are multiple "objects" that derive from this class that also define behaviors:class Car : Node{  /* ... physics code ... */  /* ... other code for updating the Node's transformation etc ... */}Now, when I create a Node purely to be rendered, I have an initializer that loads from a file (COLLADA, for example). This initializer recursively calls and creates subnodes based on the structure in the code.Now, when I create new Nodes from a node itself, I could do something like thischildren.push_back (new Node (/*...*/));However, then I have to deal with deleting the memory, and since it is intermingled with potentially static memory, and even memory that doesn' twant to be deleted when the Node is deleted, this is a bad solution.I could change the definition of the vector to just use the objects rather than pointers then do this:children.push_back (Node (/* ... */));But the problem would be that derived classes would not be able to be added? Or would I do something likechildren.push_back (*dynamic_cast<Node*> (derived));Would this copy the memory (bad, since the object will later be updated), or will it simply reference the old memory?push_back generally makes a copy though, right?Edit:searching on google gives me thisResource * r = new Resource("Wheat",1,0,0, "Farm");vResource.push_back( *r ) ;delete r;Since it makes a copy, the resource can be deleted afterwards.  This seems backwards to me, however.  Is this the recommended way to do something like this?   
Quote:Or would I do something likechildren.push_back (*dynamic_cast<Node*> (derived));Would this copy the memory (bad, since the object will later be updated), or will it simply reference the old memory?That will cause slicing... just like push_backing the node itself would. You don't want that.It sounds like you've identified the relevant issues here: you need pointers in order to leverage polymorphic behavior, but that means manual memory management. Either do the manual memory management, or use smart pointers. In the simple case of a tree, manual memory management won't be too bad... just define (via documentation and comments) who owns each pointed-to object and under what circumstances ownership changes hands, then make sure that an object deletes all the owned objects it needs to.   ;Quote:Original post by solinentI could change the definition of the vector to just use the objects rather than pointers then do this:children.push_back (Node (/* ... */));But the problem would be that derived classes would not be able to be added? Or would I do something likechildren.push_back (*dynamic_cast<Node*> (derived));Would this copy the memory (bad, since the object will later be updated), or will it simply reference the old memory?It will still slice the object. The slicing is a property of the container, not the procedure for adding elements. A vector of T is a vector of T. It cannot contain derived-from-T, because it allocates memory in (element count * T)-sized chunks, and uses T's assignment operator and copy constructor.The indirection is required, and therefore there is memory management work to be done. You can use smart pointers, or one of the Boost ptr_containers, to handle the memory management.Quote:Resource * r = new Resource("Wheat",1,0,0, "Farm");vResource.push_back( *r ) ;delete r;Since it makes a copy, the resource can be deleted afterwards.  This seems backwards to me, however.  Is this the recommended way to do something like this?This still slices the object, for the same reason. Dynamically allocating the to-be-copied object accomplishes nothing except to make more memory-management work for the programmer.   ;  If you use smart pointers you can intermingle dynamically allocated and statically allocated pointers by taking advantage of custom deleters if the smart pointer supports them. For example with shared_ptr, you can register a no-op deleter for statically allocated objects.   ;  Solutions, from best to worse: - vector<unique_ptr<Node>> - ptr_vector<Node> - vector<clone_ptr<Node>> - vector<shared_ptr<Node>>Pick one and use it. You might want to implement clone_ptr with COW if your vector doesn't have move semantics (which are emulable in C++03 with some limitations).First option obviously requires move semantics (emulated or not).A container of pointers to dynamically allocated data is always wrong.   ;  I've decided on a shared_ptr since that closest represents the semantics of what I'm doing: sharing the object between two classes.To do this, I've created addFromHeap and addFromStack classes, which add pointers to the array of shared_ptr.  However, the addFromStack adds a pointer with a noop delete.Then the remove method just calls std::vector::erase which erases the shared_ptr instance but not the object unless it is the last object.Thanks!   