Constructors (starstruck noob from C++)

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Jan 21 07:55:11 PST 2011


Theorizing: Would it be a bad idea if .dup for classes did the same
thing as normal assignment did for structs with postblit constructors?

Essentially I was thinking that code like this would do the trick:

import std.stdio;

class Widget
{
    int integral;  // field-by-field assignment
    int[] array;   // this needs a postblit
    this(uint length)
    {
        array = new int[length];
    }

    this(this)
    {
        array = array.dup;
    }
}

void main()
{
    auto w1 = new Widget(10);
    auto w2 = w1.dup;
    assert(w1.array !is w2.array);
}

I like uniformity in a language, it's much easier to get the rules
right this way. But I'm speculating whether this would even work
right..


More information about the Digitalmars-d mailing list