digitalmars.D.learn - Converting multiple inheritance code into C ++ for D language
- Jean Cesar (57/57) Feb 17 2017 import std.stdio;
- Nicholas Wilson (8/65) Feb 17 2017 Like in c#, classes on D are reference types and all methods are
- Brian Rogoff (5/8) Feb 17 2017 What's the deal with that? It seems someone made progress on this
- Adam D. Ruppe (5/7) Feb 17 2017 Interfaces + mixin templates give you something very similar to
- Jean Cesar (4/11) Feb 17 2017 Yes I saw here that it uses interface to make multiple
- biozic (6/20) Feb 18 2017 A mixin can be used to provide an base implementation for the
- wiki (74/96) Feb 18 2017 I tried to use it in the same way but I did not understand
- biozic (68/79) Feb 18 2017 There are multiple typos problems with your code. For me, the
- Jean Cesar (5/88) Feb 18 2017 This is exactly what I want this code I did to understand how
- biozic (4/8) Feb 18 2017 OK, but I guess you are aware that in this code, using interfaces
- Jean Cesar (6/15) Feb 18 2017 Yes this code is useless because it will serve as a hello world
import std.stdio; import std.string; I've been reading a bit about multi-inheritance in D, 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; }
Feb 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 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; }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.
Feb 17 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!
Feb 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.
Feb 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:Yes I saw here that it uses interface to make multiple this mixing?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.
Feb 17 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: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/b656851e5c51On Friday, 17 February 2017 at 23:11:25 UTC, Jean Cesar wrote:Yes I saw here that it uses interface to make multiple this mixing?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.
Feb 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: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; }On Friday, 17 February 2017 at 23:31:41 UTC, Adam D. Ruppe 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/b656851e5c51On Friday, 17 February 2017 at 23:11:25 UTC, Jean Cesar wrote:Yes I saw here that it uses interface to make multiple this mixing?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.
Feb 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: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 }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/b656851e5c51I 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 ..
Feb 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:This is exactly what I want this code I did to understand how in the base class you have to define it as interface. ..On Saturday, 18 February 2017 at 09:33:25 UTC, biozic wrote: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 }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/b656851e5c51I 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 ..
Feb 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 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 :)
Feb 18 2017
On Saturday, 18 February 2017 at 19:45:45 UTC, biozic wrote:On Saturday, 18 February 2017 at 19:05:14 UTC, Jean Cesar wrote:Yes this code is useless because it will serve as a hello world using multiple inheritance or else I study the language in what it supports and does not support to have examples where I can base myself and have an idea of how to do just to even understand how to implement something like this .This is exactly what I want this code I did to understand how 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 :)
Feb 18 2017