Converting multiple inheritance code into C ++ for D language
wiki via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Feb 18 04:56:51 PST 2017
On Saturday, 18 February 2017 at 09:33:25 UTC, biozic wrote:
> On Friday, 17 February 2017 at 23:35:33 UTC, Jean Cesar wrote:
>> On Friday, 17 February 2017 at 23:31:41 UTC, Adam D. Ruppe
>> wrote:
>>> On Friday, 17 February 2017 at 23:11:25 UTC, Jean Cesar wrote:
>>>> so I changed the code to use interface but how would I do so
>>>> I could use the constructor in the same way as such a C ++
>>>> code?
>>>
>>> Interfaces + mixin templates give you something very similar
>>> to multiple inheritance. You can have named functions in the
>>> mixin templates that do the work of the constructor, then
>>> call them from the real constructor.
>>
>>
>> Yes I saw here that it uses interface to make multiple
>> inheritance just like C#, but I did not understand what would
>> this mixing?
>
> A mixin can be used to provide an base implementation for the
> methods of an interface, along with data members, so that you
> don't have to define it in every class that implements the
> interface.
>
> An example : https://dpaste.dzfl.pl/b656851e5c51
I tried to use it in the same way but I did not understand
correctly because to simulate, alias in this code I had already
defined the classes as interfaces but I did not understand how
these constructors should be declared for later use ..
import std.stdio;
import std.string;
/**************************************************************************
* Source: Digital MArs D
*
* Name: StringHerancaMulti.d
*
* Concept Aplied: Herança Multipla usando Classe com 4 variáveis
string *
* Autor: Jean Zonta
*
**************************************************************************/
class Test1
{
protected string _msg1;
this( string msg1 )
{
_msg1=msg1;
}
};
interface Test2
{
protected string _msg2;
// Provide an overridable implementation
mixin template Impl()
{
this( string msg2 )
{
_msg2=msg2;
}
}
};
interface Test3
{
protected string _msg3;
// Provide an overridable implementation
mixin template Impl()
{
this( string msg3 )
{
_msg3=msg3;
}
}
};
class Test4: Test1, Test2, Test3
{
mixin Test2.Impl;
mixin Test3.Impl;
string _msg4;
this( string msg1, string msg2 , string msg3, string msg4 )
{
super(msg1,msg2,msg3);
this._msg1 = msg1;
this._msg2 = msg2;
this._msg3 = msg3;
this._msg4 = msg4;
}
override void show()
{
writeln(_msg1,_msg2,_msg3,_msg4);
}
};
void main()
{
Test4 teste = new Teste4("\n\tTeste1 ","Teste2 ","Teste3
","Teste4");
teste.show();
return 0;
}
More information about the Digitalmars-d-learn
mailing list