constructor is not callable using argument types ()

Simen Kjaeraas simen.kjaras at gmail.com
Thu Dec 6 13:34:03 PST 2012


On 2012-12-06, 20:48, Suliman wrote:

> When I should use keyword this?
> I dropped it from my class and now I can make instance of class without  
> in sych way:
>
> auto file = new GetFileName();
> file.name = "test";

Indeed. If you have not defined a constructor, the language defines one
for you, which is parameterless. It does nothing but allocate and
initialize memory.

The moment you define a constructor of your own, the compiler decides
you probably want to define the parameterless constructor yourself, or
not at all, and thus does not define it for you.

If you want both, then define both:

class MyClass {
     string name;
     this() {
         name = "Foo!";
     }
     this(string name) {
         this.name = name;
     }
}

-- 
Simen


More information about the Digitalmars-d-learn mailing list