www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Inheriting Base Constructors?

reply AJG <AJG_member pathlink.com> writes:
Hi there,

I have a base abstract class which implements, among other things, two base
constructors and a base destructor. My problem is in the derived classes.

The destructor works fine because there can only be one. Thus, when the object
is destroyed, the base destructor gets called and all is good.

The constructors, however, are giving me trouble. Specifically, the derived
class is not inheriting one of the constructors.

In the D docs, it says:

http://www.digitalmars.com//d/class.html
"If there is no constructor for a class, but there is a constructor for the base
class, a default constructor of the form: this() { } is implicitly generated."

Why won't it do that with constructors that have parameters?

Thanks,
--AJG.

PS: Another related problem: If the base class declares the default constructor
"this()" as protected, why doesn't it have any bearing in the derived classes?
Aug 12 2005
parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
 The constructors, however, are giving me trouble. Specifically, the 
 derived
 class is not inheriting one of the constructors.

Derived classes don't inherit ctors. Making an instance of a class is different than calling methods of a class.
 In the D docs, it says:

 http://www.digitalmars.com//d/class.html
 "If there is no constructor for a class, but there is a constructor for 
 the base
 class, a default constructor of the form: this() { } is implicitly 
 generated."

 Why won't it do that with constructors that have parameters?

It would be impossible for a subclass to prevent a superclass's ctor from being used to create a subclass - which doesn't make sense.
 Thanks,
 --AJG.

 PS: Another related problem: If the base class declares the default 
 constructor
 "this()" as protected, why doesn't it have any bearing in the derived 
 classes?

I'm not sure what you mean by bearing. You should be able to call it from the derived class but not from other code.
Aug 13 2005
parent reply AJG <AJG_member pathlink.com> writes:
In article <ddkqgo$2vg5$1 digitaldaemon.com>, Ben Hinkle says...
 The constructors, however, are giving me trouble. Specifically, the 
 derived
 class is not inheriting one of the constructors.

Derived classes don't inherit ctors. Making an instance of a class is different than calling methods of a class.

Ah. I got the wrong impression from what I read below. I was hoping that since: # this() {} is implicitly generated, and then that turns implicitly into: # this() { super() } That other constructors would follow. I.e. that: # this(int a) {} which would turn into: # this(int a) { super(a); } Would also be implicitly generated if the super class constained a super ctor: # this(int a) { printf("base"); } Does that make sense? Is there any other way to accomplish that? I mean, without having all derived classes have to implement the sub-ctor with the parameter just to call super(a)?
 PS: Another related problem: If the base class declares the default 
 constructor
 "this()" as protected, why doesn't it have any bearing in the derived 
 classes?

I'm not sure what you mean by bearing. You should be able to call it from the derived class but not from other code.

I meant that if the super ctor is protected, is there a way to enforce that the sub ctor remain protected? Thanks, --AJG.
Aug 13 2005
parent "Ben Hinkle" <ben.hinkle gmail.com> writes:
"AJG" <AJG_member pathlink.com> wrote in message 
news:ddmh0e$14hg$1 digitaldaemon.com...
 In article <ddkqgo$2vg5$1 digitaldaemon.com>, Ben Hinkle says...
 The constructors, however, are giving me trouble. Specifically, the
 derived
 class is not inheriting one of the constructors.

Derived classes don't inherit ctors. Making an instance of a class is different than calling methods of a class.

Ah. I got the wrong impression from what I read below. I was hoping that since: # this() {} is implicitly generated, and then that turns implicitly into: # this() { super() } That other constructors would follow. I.e. that: # this(int a) {} which would turn into: # this(int a) { super(a); } Would also be implicitly generated if the super class constained a super ctor: # this(int a) { printf("base"); } Does that make sense? Is there any other way to accomplish that? I mean, without having all derived classes have to implement the sub-ctor with the parameter just to call super(a)?

Try using default parameters to shorten the list of ctors - that's all I can think of to cut down on the number. How many are there, out of curiosity? Presumably the number of ctors is somewhere below 4 - usually 1 or 2.
 PS: Another related problem: If the base class declares the default
 constructor
 "this()" as protected, why doesn't it have any bearing in the derived
 classes?

I'm not sure what you mean by bearing. You should be able to call it from the derived class but not from other code.

I meant that if the super ctor is protected, is there a way to enforce that the sub ctor remain protected?

Nothing comes to mind.
Aug 14 2005