RedBlackTree thin wrapper initialization
Edwin van Leeuwen via Digitalmars-d
digitalmars-d at puremagic.com
Wed May 28 02:37:53 PDT 2014
On Wednesday, 28 May 2014 at 08:46:30 UTC, Rene Zwanenburg wrote:
>
> The problem is the initialization of A.tree. Short answer, move
> it to a constructor and it should work.
>
> Now what I think is going on: default values for member fields
> must be known at compile time. This is why structs can do
> something like
>
> struct S
> {
> int i = 5;
> }
>
> while they're not allowed to define a default constructor.
>
> So it appears the tree is instantiated at compile time and
> stored somewhere, and the reference to that instance is used as
> the default value for A.tree. I didn't know this was possible
> to do with reference type members, and perhaps it should be
> disallowed. This is highly counter intuitive behavior esp. for
> people coming from Java.
Thank you for the reply. Does this mean I should never initialize
classes/objects like that or is it more specific to RBT?
I guess structs have the same problem with classes/objects? That
makes struct that hold objects quite awkward to use, since you
can't define a default constructor for structs.
struct S
{
auto tree = new RedBlackTree!string();
}
PS Sorry for not posting this to the learn forum.
More information about the Digitalmars-d
mailing list