Inherits DynamicObject::clone( )


#1

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?

 


#2

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


#3

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.


#4

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!


#5

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? 


#6

Huh? No.. You'd do this:

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

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

#7

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


#8

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


#9

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


#10

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