www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Class-Inheritancing Error

reply David T. Oxygen <TKC847567 outlook.com> writes:
I wrote a piece of code like this:

```d
class Person{
	string name;
	this(string name){this.name=name;}
}
class Someone:Person{
	alias super this;
}
void main(){
	Someone x=new Someone("Bob");
}
```
And then,I got a Error-Message:
```
ab.d(6): Error: variable name expected after type `super`, not 
`this`
     alias super this;
                 ^
ab.d(6):        `this` is a keyword, perhaps append `_` to make 
it an identifier

```
Who knows why I can't use `alias this` there? Please tell 
me,thanks very much. I'm waiting for you.
Jul 27
parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Sun, Jul 27, 2025 at 11:08:33AM +0000, David T. Oxygen via
Digitalmars-d-learn wrote:
 I wrote a piece of code like this:
 
 ```d
 class Person{
 	string name;
 	this(string name){this.name=name;}
 }
 class Someone:Person{
 	alias super this;
 }
 void main(){
 	Someone x=new Someone("Bob");
 }
 ```
 And then,I got a Error-Message:
 ```
 ab.d(6): Error: variable name expected after type `super`, not `this`
     alias super this;
                 ^
 ab.d(6):        `this` is a keyword, perhaps append `_` to make it an
 identifier
 
 ```
 Who knows why I can't use `alias this` there? Please tell me,thanks
 very much. I'm waiting for you.
Can you explain more why do you want to alias super to this? A derived class already inherits its base class's members, there's no need to explicitly alias it. T -- Fact is stranger than fiction.
Jul 27
parent user1234 <user1234 12.de> writes:
On Sunday, 27 July 2025 at 15:14:03 UTC, H. S. Teoh wrote:
 On Sun, Jul 27, 2025 at 11:08:33AM +0000, David T. Oxygen via 
 Digitalmars-d-learn wrote:
 I wrote a piece of code like this:
 
 ```d
 class Person{
 	string name;
 	this(string name){this.name=name;}
 }
 class Someone:Person{
 	alias super this;
 }
 void main(){
 	Someone x=new Someone("Bob");
 }
 ```
 And then,I got a Error-Message:
 ```
 ab.d(6): Error: variable name expected after type `super`, not 
 `this`
     alias super this;
                 ^
 ab.d(6):        `this` is a keyword, perhaps append `_` to 
 make it an
 identifier
 
 ```
 Who knows why I can't use `alias this` there? Please tell 
 me,thanks
 very much. I'm waiting for you.
Can you explain more why do you want to alias super to this? A derived class already inherits its base class's members, there's no need to explicitly alias it. T
Simply because because if OP writes ``` class Person{ string name; this(string name){this.name=name;} } class Someone:Person{ } void main(){ Someone x=new Someone("Bob"); } ``` then he gets rewarded with
 Error: class `Someone` cannot implicitly generate a default 
 constructor when base class `Person` is missing a default 
 constructor
Jul 27