0

I have a scene graph, all nodes deriving from a base class AbstractNode. AbstractNode has a registerChild member function:

registerChild<T>(string name, shared_ptr<AbstractNode> * childMember)

used for registering the children in a standard way (so that they can be listed and modified in the base class interface). It basically adds the name/child_pointer pair to a hash.

So for instance the class material will be declared like that (shortened):

class Material : public AbstractNode
{
public:
    Material() { registerChild(&color); } 

private:
    std_shared<Color> color;
}

Color being another subclass of AbstractNode.

Now I would like to implement a copy constructor for AbstractNode. It must copy the list of registered children, and update the child member pointers. I would like to avoid reimplementing the copy constructor in all base classes. Is this possible?

I have thought of working on pointer offsets, between this and the child pointers. But are these guaranteed to be constant between two instances? It seems like a big hack to me... (And I'm even sure that this is not guaranteed when subclassing)

Thanks,

Etienne

galinette
  • 8,896
  • 2
  • 36
  • 87
  • What on earth is `AbstractNode * shared_ptr` supposed to mean? – Paul Evans Sep 12 '13 at 11:26
  • Nothing. That's fixed... – galinette Sep 12 '13 at 11:48
  • I have a workaround : moving all "registerChild" calls from the derived class constructor to a "registerAll" virtual function. Then this one is called at the first call to a children access function. Not perfect, but working. Any idea on updating the pointer table upon copying, without asking to the derived class author to write more code, would be appreciated! – galinette Sep 12 '13 at 12:12
  • Whats the purpose of `shared_ptr * childMember`? A pointer to a `std::shared_ptr`????! – Manu343726 Sep 12 '13 at 14:15
  • Yes, or if you want to put it simply, a double pointer AbstractNode**, but shared at one stage. Pointers to pointers are commonly used for modifying the object pointed to. You've never used a double pointer????! You've never used shared pointers to avoid memory leaks and dangling pointers????! – galinette Sep 12 '13 at 14:58

0 Answers0