Inherits DynamicObject::clone( )

Hi, 

I have a class that inherits from DynamicObject. I want to override the virtual clone( ) function. Is there an obvious way to reuse DynamicObject::clone( ) or do i'm forced to copy and paste its code in my own function?

 

Oops, properties is private! I can not manage the copy myself. 

Oops (again).

That seems to do the job (but not efficiently):

    virtual Ptr clone( ) {

        Ptr base = DynamicObject::clone( );

        Ptr newCopy = new Oizo( );

        newCopy->getProperties( ) = base->getProperties( );

        return newCopy;

    }

Sorry for C++ newbish.

Hmm.. Good point, actually. DynamicObject would need a copy constructor to be able to clone a subclass of it, and it doesn't currently have one - will add one!

Do you mean that now i have to "copy construct" my (derived) class (passing a clone of the DynamicObject subclass) instead of overriding the clone method? 

Huh? No.. You'd do this:

struct foo  : public DynamicObject
{
    foo (const foo& other) : DynamicObject (other) {}

    DynamicObject::Ptr clone() override  { return new foo (*this); }
};

But in that case (supposing that my class could define a copy ctor) the copy ctor of NamedValueSet (implied in the copy ctor of DynamicObject) calls the "addCopyOfList" that doesn't explicitly clone the value of the linklist as DynamicObject::clone does.

That implies base and derived clone function to get a different behaviours. Am i wrong?

As a workaround, i currently do:

Foo(const DynamicObject& o) : DynamicObject(o) {  }

DynamicObect::Ptr clone( ) { return new Foo(*(DynamicObject::clone( ))); }

But that's not elegant, not efficient (and i'm even not sure that it is really correct).

Anyway, it doesn't matter. I make experiments with the API. I don't need that right now.

I've added a cloneAllProperties method now, which should make it simple to do what you're trying to do there.

Great! And consistent now with the D.R.Y. mantra ;-)