Jump to page: 1 2
Thread overview
constructor inheritance
Mar 04, 2008
Elwis
Mar 04, 2008
Simen Kjaeraas
Mar 04, 2008
Elwis
Mar 04, 2008
Extrawurst
Mar 04, 2008
Ary Borenszweig
Mar 04, 2008
Elwis
Mar 04, 2008
Simen Kjaeraas
Mar 04, 2008
Robert Fraser
Mar 04, 2008
Simen Kjaeraas
Mar 04, 2008
Sean Kelly
Mar 05, 2008
Ary Borenszweig
Mar 05, 2008
Janice Caron
Mar 05, 2008
Ary Borenszweig
Mar 05, 2008
Elwis
Mar 04, 2008
Sean Kelly
Mar 04, 2008
Sean Kelly
March 04, 2008
Hi.
Is there any way to make constructor inherited automaticaly. I has many classes that have to have the same constructor, and I'd like not to copy it for each one.
March 04, 2008
On Tue, 04 Mar 2008 19:32:11 +0100, Elwis <elwispl@gmail.com> wrote:

> Hi.
> Is there any way to make constructor inherited automaticaly. I has many classes that have to have the same constructor, and I'd like not to copy it for each one.

A class constructor in D /is/ inherited automatically. From the spec (http://www.digitalmars.com/d/2.0/class.html):
	"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."
If that for some reason does not work (it does for me), file a bug report, and use an explicit call to super() in your constructors.

If you're speaking of several non-related classes having identical constructors, I'd do it with a mixin.

	template myConstructor
	{
		// do stuff here
	}

	this()
	{
		mixin(myConstructor);
	}

If I have somehow completely missed your point, please do tell.

-- Simen
March 04, 2008
I might have described my problem unclearly.

I has root class and it has some constructor. One of those just calls one of its methods. I'd like not to copy declaration of this constructor in all of its children.
March 04, 2008
If by child of a class you mean a derived class who inherits from root please read the earlier post of Simen again, there is your answer.

Elwis schrieb:
> I might have described my problem unclearly.
>
> I has root class and it has some constructor. One of those just calls one of its methods. I'd like not to copy declaration of this constructor in all of its children.
>   
March 04, 2008
Not currently.  I posted a proposal for this maybe 6-8 months ago and got no response.  I'd love to have it in D 2.0.


Sean
March 04, 2008
Elwis wrote:
> I might have described my problem unclearly.
> 
> I has root class and it has some constructor. One of those just calls one of its methods. I'd like not to copy declaration of this constructor in all of its children.

Do you mean you don't want to have to do this?

class Parent {

  this(int x, int y) {
    // some code
  }

}

class Child : Parent {

  // I wish the compiler would add the this(int x, int y) constructor
  // automatically for me here

}
March 04, 2008
== Quote from Sean Kelly (sean@invisibleduck.org)'s article
> Not currently.  I posted a proposal for this maybe 6-8 months ago and got no response.  I'd love to have
it
> in D 2.0.

Here's the link:

http://www.digitalmars.com/d/archives/digitalmars/D/Inheriting_constructors_54088.html


Sean
March 04, 2008
Ary Borenszweig Wrote:

> Elwis wrote:
> > I might have described my problem unclearly.
> > 
> > I has root class and it has some constructor. One of those just calls one of its methods. I'd like not to copy declaration of this constructor in all of its children.
> 
> Do you mean you don't want to have to do this?
> 
> class Parent {
> 
>    this(int x, int y) {
>      // some code
>    }
> 
> }
> 
> class Child : Parent {
> 
>    // I wish the compiler would add the this(int x, int y) constructor
>    // automatically for me here
> 
> }

It doesn't work. Maybe it isn't supported by GDC?
March 04, 2008
On Tue, 04 Mar 2008 21:59:35 +0100, Elwis <elwispl@gmail.com> wrote:

> Ary Borenszweig Wrote:
>
>> Elwis wrote:
>> > I might have described my problem unclearly.
>> >
>> > I has root class and it has some constructor. One of those just calls  
>> one of its methods. I'd like not to copy declaration of this constructor in all of its children.
>>
>> Do you mean you don't want to have to do this?
>>
>> class Parent {
>>
>>    this(int x, int y) {
>>      // some code
>>    }
>>
>> }
>>
>> class Child : Parent {
>>
>>    // I wish the compiler would add the this(int x, int y) constructor
>>    // automatically for me here
>>
>> }
>
> It doesn't work. Maybe it isn't supported by GDC?


It is not supported by D at all. At least not at the moment. Like I said, your best bet is templates.
Something like

template constructors()
{
	this()
	{
		// do stuff here
	}
	
	this(int x, int y)
	{
		// do stuff here
	}
}

class Parent
{
	mixin constructors;
}

class Child
{
	mixin constructors;
}
March 04, 2008
Simen Kjaeraas wrote:
> On Tue, 04 Mar 2008 21:59:35 +0100, Elwis <elwispl@gmail.com> wrote:
> 
>> Ary Borenszweig Wrote:
>>
>>> Elwis wrote:
>>> > I might have described my problem unclearly.
>>> >
>>> > I has root class and it has some constructor. One of those just 
>>> calls one of its methods. I'd like not to copy declaration of this constructor in all of its children.
>>>
>>> Do you mean you don't want to have to do this?
>>>
>>> class Parent {
>>>
>>>    this(int x, int y) {
>>>      // some code
>>>    }
>>>
>>> }
>>>
>>> class Child : Parent {
>>>
>>>    // I wish the compiler would add the this(int x, int y) constructor
>>>    // automatically for me here
>>>
>>> }
>>
>> It doesn't work. Maybe it isn't supported by GDC?
> 
> 
> It is not supported by D at all. At least not at the moment. Like I said, your best bet is templates.
> Something like
> 
> template constructors()
> {
>     this()
>     {
>         // do stuff here
>     }
>         this(int x, int y)
>     {
>         // do stuff here
>     }
> }
> 
> class Parent
> {
>     mixin constructors;
> }
> 
> class Child
> {
>     mixin constructors;
> }

How about just:

class Child : Parent
{
    this(int x, int y)
    {
        super(x, y);
        // Do anything else here if you want
    }
}
« First   ‹ Prev
1 2