initializing struct containing user defined type

kdevel kdevel at vogtner.de
Fri Feb 18 14:19:58 UTC 2022


Instead of using string I want to use A in the definition of B:

    struct A {
       string s;
       this (string s)
       {
          this.s = s;
       }
    }

    struct B {
       A a;
       A b;
    }

Alas I have some difficulty initializing a B in the accustomed 
manner (b3):

    void main ()
    {
       auto b0 = B (A ("A"), A ("B")); // as expected but I don't 
want
                                       // to change the client code

    //   B b1 = { {"A"}, {"B"} }; // works if A has no (explicit) 
ctor
                                  // but I don't want to write the 
braces

       B b2 = {"A", "B"}; // works but why?

    //   auto b3 = B ("A", "B"); // Error: cannot implicitly 
convert
    // expression `"A"` of type `string` to `A`
    }

Hasn't this been treated here before?


More information about the Digitalmars-d-learn mailing list