Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
September 07, 2013 Do constructors in D support the privacy keyword? | ||||
---|---|---|---|---|
| ||||
Do constructors in D support a privacy keyword? I'm guessing not because if i declare one like this: class T { private this() { } } i can still instantiate the class like this: auto x = new T(); and there is no error thrown. Am i right in thinking the privacy keyword is ignored? |
September 07, 2013 Re: Do constructors in D support the privacy keyword? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Saturday, 7 September 2013 at 18:37:11 UTC, Gary Willoughby
wrote:
> Do constructors in D support a privacy keyword? I'm guessing not because if i declare one like this:
>
> class T
> {
> private this()
> {
> }
> }
>
> i can still instantiate the class like this:
>
> auto x = new T();
>
> and there is no error thrown. Am i right in thinking the privacy keyword is ignored?
In D, private symbols are accessible throughout their module. Try
instantiating the class from another module and you should get an
error.
|
September 07, 2013 Re: Do constructors in D support the privacy keyword? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Sat, Sep 07, 2013 at 08:37:05PM +0200, Gary Willoughby wrote: > Do constructors in D support a privacy keyword? I'm guessing not because if i declare one like this: > > class T > { > private this() > { > } > } > > i can still instantiate the class like this: > > auto x = new T(); > > and there is no error thrown. Am i right in thinking the privacy keyword is ignored? Did you put the 'new' line in the same file as class T? In D, 'private' means 'private to this module', not 'private to this class' as in C++. So 'new T()' should be compilable inside the same module, but not outside. If it still compiles outside, I'd say file a bug. T -- A bend in the road is not the end of the road unless you fail to make the turn. -- Brian White |
September 07, 2013 Re: Do constructors in D support the privacy keyword? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Saturday, 7 September 2013 at 18:49:43 UTC, H. S. Teoh wrote:
> On Sat, Sep 07, 2013 at 08:37:05PM +0200, Gary Willoughby wrote:
>> Do constructors in D support a privacy keyword? I'm guessing not
>> because if i declare one like this:
>>
>> class T
>> {
>> private this()
>> {
>> }
>> }
>>
>> i can still instantiate the class like this:
>>
>> auto x = new T();
>>
>> and there is no error thrown. Am i right in thinking the privacy
>> keyword is ignored?
>
> Did you put the 'new' line in the same file as class T? In D, 'private'
> means 'private to this module', not 'private to this class' as in C++.
> So 'new T()' should be compilable inside the same module, but not
> outside. If it still compiles outside, I'd say file a bug.
>
>
> T
It was inside the same module. I didn't realise that about private. Thanks for the clarification.
|
September 08, 2013 Re: Do constructors in D support the privacy keyword? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | In same module "private" acts like "friend" in C++. |
Copyright © 1999-2021 by the D Language Foundation