Jump to page: 1 2
Thread overview
Converting multiple inheritance code into C ++ for D language
Feb 17, 2017
Jean Cesar
Feb 17, 2017
Nicholas Wilson
Feb 18, 2017
Brian Rogoff
Feb 17, 2017
Adam D. Ruppe
Feb 17, 2017
Jean Cesar
Feb 18, 2017
biozic
Feb 18, 2017
wiki
Feb 18, 2017
biozic
Feb 18, 2017
Jean Cesar
Feb 18, 2017
biozic
Feb 19, 2017
Jean Cesar
February 17, 2017
import std.stdio;
import std.string;

I've been reading a bit about multi-inheritance in D, but I have to use interface like C # to use multiple inheritance, but I have the code in C ++ that I've been testing to understand how it would be possible to implement multi-inheritance constructor despite seemingly It does not represent many things 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?

Test1,2,3 would be the name of the class constructors in C ++ how to port completely to D so that it works the same way?

import std.stdio;
import std.string;

class Test1
{
 protected:
  std::string _msg1;
   public:
   Test1( std::string msg1 ):
 _msg1( msg1 ){}
};

class Test2
{
 protected:
  std::string _msg2;
   public:
   Test2( std::string msg2 ):
 _msg2( msg2 ){}
};

class Test3
{
 protected:
  std::string _msg3;
   public:
   Test3( std::string msg3 ):
 _msg3( msg3 ){}
};

class Test4: public Test1, public Test2, public Test3
{
 std::string _msg4;
  public:
  Test4( std::string msg1, std::string msg2 , std::string msg3, std::string msg4 ):
  Test1( msg1 ), Test2( msg2 ), Test3( msg3 ), _msg4( msg4 ){ }
 void show();
};

void Test4::show()
{
  std::cout << this->_msg1 << this->_msg2 << this->_msg3 << this->_msg4 << "\n\n";
}

int main()
{
 Test4 teste("\n\tTeste1 ","Teste2 ","Teste3 ","Teste4");
  teste.show();
 return 0;
}
February 17, 2017
On Friday, 17 February 2017 at 23:11:25 UTC, Jean Cesar wrote:
> import std.stdio;
> import std.string;
>
> I've been reading a bit about multi-inheritance in D, but I have to use interface like C # to use multiple inheritance, but I have the code in C ++ that I've been testing to understand how it would be possible to implement multi-inheritance constructor despite seemingly It does not represent many things 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?
>
> Test1,2,3 would be the name of the class constructors in C ++ how to port completely to D so that it works the same way?
>
> import std.stdio;
> import std.string;
>
> class Test1
> {
>  protected:
>   std::string _msg1;
>    public:
>    Test1( std::string msg1 ):
>  _msg1( msg1 ){}
> };
>
> class Test2
> {
>  protected:
>   std::string _msg2;
>    public:
>    Test2( std::string msg2 ):
>  _msg2( msg2 ){}
> };
>
> class Test3
> {
>  protected:
>   std::string _msg3;
>    public:
>    Test3( std::string msg3 ):
>  _msg3( msg3 ){}
> };
>
> class Test4: public Test1, public Test2, public Test3
> {
>  std::string _msg4;
>   public:
>   Test4( std::string msg1, std::string msg2 , std::string msg3, std::string msg4 ):
>   Test1( msg1 ), Test2( msg2 ), Test3( msg3 ), _msg4( msg4 ){ }
>  void show();
> };
>
> void Test4::show()
> {
>   std::cout << this->_msg1 << this->_msg2 << this->_msg3 << this->_msg4 << "\n\n";
> }
>
> int main()
> {
>  Test4 teste("\n\tTeste1 ","Teste2 ","Teste3 ","Teste4");
>   teste.show();
>  return 0;
> }

Like in c#, classes on D are reference types and all methods are virtual unless marked final. Also D only allows single inheritance for data members, but you can multiplly inherit methods from interfaces (think abstract classes).

Something like this would be a goods use for struct multiple alias this, except that we haven't implemented that yet unfortunately.
February 17, 2017
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.
February 17, 2017
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?
February 18, 2017
On Friday, 17 February 2017 at 23:24:57 UTC, Nicholas Wilson wrote:
> Something like this would be a goods use for struct multiple alias this, except that we haven't implemented that yet unfortunately.

What's the deal with that? It seems someone made progress on this issue 2 years ago and then vanished. It's a fairly significant feature that's never been implemented!

February 18, 2017
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
February 18, 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;
}

February 18, 2017
On Saturday, 18 February 2017 at 12:56:51 UTC, wiki wrote:
> On Saturday, 18 February 2017 at 09:33:25 UTC, biozic wrote:
>> 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 ..

There are multiple typos problems with your code. For me, the main problem would be that this code is using OOP the wrong way, but maybe this code doesn't represent what you actually want to do... Anyway, see a corrected version below.

import std.stdio;

class Test1
{
    protected string _msg1;

    this(string msg1)
    {
        _msg1 = msg1;
    }
} // No semicolon

interface Test2
{
    // Interfaces can't have data members

    // This template could actually be out of the interface.
    // I just put it here because it's more clear that it's related to Test2.
    mixin template Impl()
    {
        protected string _msg2; // Data member is inside the template

        // This function is not a constructor. Only the class implementing
        // the interface will have one.
        void thisTest2(string msg2)
        {
            _msg2 = msg2;
        }
    }
}

interface Test3
{
    mixin template Impl()
    {
        protected string _msg3;
        void thisTest3(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);  // Calls the constructor of Test1
        thisTest2(msg2); // Use interface Test2
        thisTest3(msg3); // Use interface Test3
        this._msg4 = msg4; // Test4 implementation
    }

    void show() // Don't use override here
    {
        writeln(_msg1, _msg2, _msg3, _msg4);
    }
}

void main()
{
    auto teste = new Test4("\n\tTeste1 ", "Teste2 ", "Teste3 ", "Teste4");
    teste.show();
    // No explicit return is required
}
February 18, 2017
On Saturday, 18 February 2017 at 16:27:51 UTC, biozic wrote:
> On Saturday, 18 February 2017 at 12:56:51 UTC, wiki wrote:
>> On Saturday, 18 February 2017 at 09:33:25 UTC, biozic wrote:
>>> 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 ..
>
> There are multiple typos problems with your code. For me, the main problem would be that this code is using OOP the wrong way, but maybe this code doesn't represent what you actually want to do... Anyway, see a corrected version below.
>
> import std.stdio;
>
> class Test1
> {
>     protected string _msg1;
>
>     this(string msg1)
>     {
>         _msg1 = msg1;
>     }
> } // No semicolon
>
> interface Test2
> {
>     // Interfaces can't have data members
>
>     // This template could actually be out of the interface.
>     // I just put it here because it's more clear that it's related to Test2.
>     mixin template Impl()
>     {
>         protected string _msg2; // Data member is inside the template
>
>         // This function is not a constructor. Only the class implementing
>         // the interface will have one.
>         void thisTest2(string msg2)
>         {
>             _msg2 = msg2;
>         }
>     }
> }
>
> interface Test3
> {
>     mixin template Impl()
>     {
>         protected string _msg3;
>         void thisTest3(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);  // Calls the constructor of Test1
>         thisTest2(msg2); // Use interface Test2
>         thisTest3(msg3); // Use interface Test3
>         this._msg4 = msg4; // Test4 implementation
>     }
>
>     void show() // Don't use override here
>     {
>         writeln(_msg1, _msg2, _msg3, _msg4);
>     }
> }
>
> void main()
> {
>     auto teste = new Test4("\n\tTeste1 ", "Teste2 ", "Teste3 ", "Teste4");
>     teste.show();
>     // No explicit return is required
> }

This is exactly what I want this code I did to understand how would apply multiple inheritance in D, C # also process using interfaces but the difference from C # to D is that C # already in the base class you have to define it as interface. ..
February 18, 2017
On Saturday, 18 February 2017 at 19:05:14 UTC, Jean Cesar wrote:
> This is exactly what I want this code I did to understand how would apply multiple inheritance in D, C # also process using interfaces but the difference from C # to D is that C # already in the base class you have to define it as interface. ..

OK, but I guess you are aware that in this code, using interfaces and the pseudo-multiple-inheritance is pointless! You could just ditch them and use regular methods :)
« First   ‹ Prev
1 2