www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - private constructors and inheritance

reply "John Colvin" <john.loughran.colvin gmail.com> writes:
//blah1.d

class A
{
     private this(){}
}


//blah2.d

import blah1;

class B : A {}


$ dmd blah1.d blah2.d -lib
Error: constructor blah1.A.this is not accessible from module 
blah2

Can someone explain why this can't/doesn't work? Thanks.
Apr 29 2014
next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Tuesday, 29 April 2014 at 13:59:30 UTC, John Colvin wrote:
 //blah1.d

 class A
 {
     private this(){}
 }


 //blah2.d

 import blah1;

 class B : A {}


 $ dmd blah1.d blah2.d -lib
 Error: constructor blah1.A.this is not accessible from module 
 blah2

 Can someone explain why this can't/doesn't work? Thanks.
Also, I noticed that adding a blank constructor ( this(){} ) to B didn't help, but adding a blank template constructor ( this()(){} ) makes the error go away.
Apr 29 2014
parent reply "Dicebot" <public dicebot.lv> writes:
http://dlang.org/class.html#constructors

"If no call to constructors via this or super appear in a 
constructor, and the base class has a constructor, a call to 
super() is inserted at the beginning of the constructor."

The fact that call to base constructor is not inserted into 
templated this()() looks like a bug to me.
Apr 29 2014
parent Andrej Mitrovic via Digitalmars-d-learn writes:
On 4/29/14, Dicebot via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:
 The fact that call to base constructor is not inserted into
 templated this()() looks like a bug to me.
Just found this, and it might be related: https://issues.dlang.org/show_bug.cgi?id=5770
Apr 29 2014
prev sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Tue, 29 Apr 2014 13:59:28 +0000
John Colvin via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:
 Can someone explain why this can't/doesn't work? Thanks.
hm. why it should? there is no 'default' constructors in D, and you specifially made explicit one private (i.e. not visible outside the module). and adding constructor to B inherits 'private' (not sure if it is right though). but templated one generates constructor code 'on demand', w/o 'private'. i think that public constructor in B should work, so this may be bug. will wait for somebody more expirienced in language to jump in before filling bug report though.
Apr 29 2014
parent "Dicebot" <public dicebot.lv> writes:
On Tuesday, 29 April 2014 at 18:51:15 UTC, ketmar via 
Digitalmars-d-learn wrote:
 On Tue, 29 Apr 2014 13:59:28 +0000
 John Colvin via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:
 Can someone explain why this can't/doesn't work? Thanks.
hm. why it should? there is no 'default' constructors in D, and you specifially made explicit one private (i.e. not visible outside the module). and adding constructor to B inherits 'private' (not sure if it is right though). but templated one generates constructor code 'on demand', w/o 'private'. i think that public constructor in B should work, so this may be bug. will wait for somebody more expirienced in language to jump in before filling bug report though.
No it shouldn't. Private != protected. If A constructor is private, no one outside of same module can access it, not even B which is direct descendant.
Apr 29 2014