Thread overview
ctor cannot be private?
Mar 27, 2004
J Anderson
March 27, 2004
i cannot understand why i can construct class mike from an outside module.
the ctor is made private to turn impossible to construct it... but unexpectdly i can construct it!? why?


code example:

class mike : Singleton!(mike) {
private:
    this()
    {
        printf("this should be impossible to execute from outside\n");
    }
    ~this()
    {
    }

public:
    void func()
    {
        printf("mike: func\n");
     }
}

thanks,
Miguel Ferreira Simões


March 27, 2004
Miguel Ferreira Simões wrote:

> i cannot understand why i can construct class mike from an outside module.
> the ctor is made private to turn impossible to construct it... but unexpectdly i can construct it!? why?
>  code example:
>  class mike : Singleton!(mike) {
> private:     this()
>     {
>         printf("this should be impossible to execute from outside\n");
>     }
>     ~this()
>     {
>     }
>  public:
>     void func()
>     {         printf("mike: func\n");
>      }
> }
> thanks,
> Miguel Ferreira Simões

In D a method (or constructor) inherits the same visibility as the method it is overloading and you can't change it.  Therefore, I think its got something to do with object's default constructor.  Anyway it's a design issue you'll need to take up with Walter.

-- 
-Anderson: http://badmama.com.au/~anderson/
March 28, 2004
thanks... for the answer!
but that seems a little bit weird, because if all the classes are derived from the Object class and
all them have a constructor, that means that it is always possible to construct a class, even if the designer
does not want to. are you sure that it works the way you described? walter... join the thread.

ps: i want a private constructor!!!!

best regards,
Miguel Ferreira Simões