Class copy

Satoshi via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Nov 10 08:44:24 PST 2016


Hello,
how can I copy class when I have pointer to the parent of it?

eg.

class Foo {
     int m_a;

     this(const(Foo) other) {
         m_a = other.m_a;
     }
}

class Bar : Foo {
     int m_b;

     this(const(Bar) other) {
         super(other);

         m_b = other.m_b;
     }

     R dup(this R)() {
         void* data = cast(void *)typeid(this).create();
         data[0 .. __traits(classInstanceSize, R)] = 
typeid(R).init[];

         auto ret = cast(R)data;
         ret.__ctor(this);

         return ret;
     }
}



Foo foo = new Bar;

Foo copyOfFoo = foo.dup; // ??


But this implementation of dup doesn't work.
Thanks


More information about the Digitalmars-d-learn mailing list