D vs C++ classes?

mw mingwu at gmail.com
Tue Jun 22 15:14:02 UTC 2021


On Tuesday, 22 June 2021 at 14:54:42 UTC, jmh530 wrote:
> On Tuesday, 22 June 2021 at 13:42:30 UTC, mw wrote:
>> [snip]
>>>
>>> However, I was thinking what happens if you do something like
>>> A ac = new C();
>>>
>>> How would the compiler handle that?
>>
>> That language is called Eiffel, and in such case, C become 
>> abstract class, as I explained in this post:
>>
>> https://forum.dlang.org/post/ztawxnwydbxiymcqvzhr@forum.dlang.org
>>
>>
>> https://forum.dlang.org/post/ztawxnwydbxiymcqvzhr@forum.dlang.org
>
> Thanks, the name of the language wasn't coming to me but I 
> recall the prior discussion.
>
> So if `C` becomes abstract, then the above line is prevented 
> because you cannot instantiate an abstract class (correct? I

That's right.

> can't test it on run.dlang.org right now and I'm not sure if it 
> is `C c = new C()` that is prevented or this one). That's kind 
> of limiting, no?


Not at all. First it's by the programmer's design (if s/he 
chooses to do it in this way).

Second: to solve diamond problem, undefine / rename a feature 
usually happen in D:

Class D : B(disable a), C {} // so using C.a()

Class D : B, C(disable a) {} // so using B.a()


Note: this resolution in D, make A B C D all usable, non of them 
is abstract.


Please check my github example.

> Does that have any separate implications for:
> ```d
> A ad = new D();
> B bd = new D();
> C cd = new D();
> D dd = new D();
> ```
> My thought would be that just `cd` would be prevented.

In your example, yes.



More information about the Digitalmars-d mailing list