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
  • Class copy Satoshi via Digitalmars-d-learn

Reply via email to