| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
January 08, 2014 inheritance from abstract broken? | ||||
|---|---|---|---|---|
| ||||
Greetings everyone,
i have the code where i have to keep pointers to so storage(context dependent stuff), so i tried declare abstract class with ctors and it didn't worked.
------- [code]
class MyContext;
abstract class MyObject
{
// yet another bug? all descendants errors no default ctor...
this() {}
this(MyContext mc)
{
}
MyContext _owner;
}
class MyObjectA : MyObject
{ ... }
//inside MyContext.CreateObj
MyObject CreateMyObj()
{
auto obj = new MyObjectA(this) // two errors produced
... do something with obj before returning it ...
return obj;
}
errors:
-----------
Error: no constructor for MyObjectA
Error: constructor MyObjectA.this () is not callable using argument types (MyContext)
--------
am i wrong about abstract classes usage in D or this is a bug?
| ||||
January 08, 2014 Re: inheritance from abstract broken? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to evilrat | On 1/8/2014 9:22 PM, evilrat wrote:
> Greetings everyone,
>
> i have the code where i have to keep pointers to so storage(context
> dependent stuff), so i tried declare abstract class with ctors and it
> didn't worked.
>
> ------- [code]
>
> class MyContext;
>
> abstract class MyObject
> {
> // yet another bug? all descendants errors no default ctor...
> this() {}
>
> this(MyContext mc)
> {
> }
>
> MyContext _owner;
> }
>
> class MyObjectA : MyObject
> { ... }
>
> //inside MyContext.CreateObj
> MyObject CreateMyObj()
> {
> auto obj = new MyObjectA(this) // two errors produced
>
> ... do something with obj before returning it ...
> return obj;
> }
>
> errors:
> -----------
> Error: no constructor for MyObjectA
> Error: constructor MyObjectA.this () is not callable using argument
> types (MyContext)
> --------
>
> am i wrong about abstract classes usage in D or this is a bug?
You're constructing a MyObjectA instance, not a MyObject, so the compiler is looking for a constructor in MyObjectA that takes a MyContext param.
class MyObjectA : MyObject {
this(MyContext mc) {
super(mc);
}
}
| |||
January 08, 2014 Re: inheritance from abstract broken? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | On Wednesday, 8 January 2014 at 12:29:48 UTC, Mike Parker wrote:
> On 1/8/2014 9:22 PM, evilrat wrote:
>> Greetings everyone,
>>
>> i have the code where i have to keep pointers to so storage(context
>> dependent stuff), so i tried declare abstract class with ctors and it
>> didn't worked.
>>
>> ------- [code]
>>
>> class MyContext;
>>
>> abstract class MyObject
>> {
>> // yet another bug? all descendants errors no default ctor...
>> this() {}
>>
>> this(MyContext mc)
>> {
>> }
>>
>> MyContext _owner;
>> }
>>
>> class MyObjectA : MyObject
>> { ... }
>>
>> //inside MyContext.CreateObj
>> MyObject CreateMyObj()
>> {
>> auto obj = new MyObjectA(this) // two errors produced
>>
>> ... do something with obj before returning it ...
>> return obj;
>> }
>>
>> errors:
>> -----------
>> Error: no constructor for MyObjectA
>> Error: constructor MyObjectA.this () is not callable using argument
>> types (MyContext)
>> --------
>>
>> am i wrong about abstract classes usage in D or this is a bug?
>
> You're constructing a MyObjectA instance, not a MyObject, so the compiler is looking for a constructor in MyObjectA that takes a MyContext param.
>
> class MyObjectA : MyObject {
> this(MyContext mc) {
> super(mc);
> }
> }
ok. gotta using mixins :(
| |||
January 08, 2014 Re: inheritance from abstract broken? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to evilrat | On 2014-01-08 13:22, evilrat wrote: > Greetings everyone, > > i have the code where i have to keep pointers to so storage(context > dependent stuff), so i tried declare abstract class with ctors and it > didn't worked. > > ------- [code] > > class MyContext; > > abstract class MyObject > { > // yet another bug? all descendants errors no default ctor... > this() {} > > this(MyContext mc) > { > } > > MyContext _owner; > } > > class MyObjectA : MyObject > { ... } > > //inside MyContext.CreateObj > MyObject CreateMyObj() > { > auto obj = new MyObjectA(this) // two errors produced > > ... do something with obj before returning it ... > return obj; > } > > errors: > ----------- > Error: no constructor for MyObjectA > Error: constructor MyObjectA.this () is not callable using argument > types (MyContext) > -------- > > am i wrong about abstract classes usage in D or this is a bug? Unfortunately constructors are not inherited. -- /Jacob Carlborg | |||
January 08, 2014 Re: inheritance from abstract broken? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to evilrat | On Wednesday, 8 January 2014 at 12:40:39 UTC, evilrat wrote:
> ok. gotta using mixins :(
Just use another ctor.
class MyObjectA : MyObject
{
this(MyContext mc)
{
super(mc);
}
}
| |||
January 08, 2014 Re: inheritance from abstract broken? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Wednesday, 8 January 2014 at 14:01:43 UTC, Gary Willoughby wrote:
> On Wednesday, 8 January 2014 at 12:40:39 UTC, evilrat wrote:
>> ok. gotta using mixins :(
>
> Just use another ctor.
>
> class MyObjectA : MyObject
> {
> this(MyContext mc)
> {
> super(mc);
> }
> }
i do this in template. just a bit sad using mixin everywhere.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply