digitalmars.D.learn - Classes. C++ to D
- Dennis Ritchie (23/23) May 03 2015 Hi,
- Meta (5/28) May 03 2015 This is not really doable right now in D. You can forward the
- Dennis Ritchie (7/36) May 03 2015 Thanks.
- Adam D. Ruppe (17/17) May 03 2015 I'd make class A and class B into mixin templates instead.
- QAston (9/32) May 03 2015 If you want to learn a language properly, translating the idioms
Hi, How can I rewrite this code to the D? ----- #include <string> #include <iostream> class A { public: std::string a() { return std::string("foo"); } }; class B { public: std::string b(){ return std::string("bar"); } }; class C : public A, public B {}; int main () { C c; std::cout << c.a() << c.b() << std::endl; return 0; }
May 03 2015
On Sunday, 3 May 2015 at 17:35:42 UTC, Dennis Ritchie wrote:Hi, How can I rewrite this code to the D? ----- #include <string> #include <iostream> class A { public: std::string a() { return std::string("foo"); } }; class B { public: std::string b(){ return std::string("bar"); } }; class C : public A, public B {}; int main () { C c; std::cout << c.a() << c.b() << std::endl; return 0; }This is not really doable right now in D. You can forward the function calls to a and b easily enough, but you can't inherit from more than one class. Once the multiple alias this patch gets merged you will be able to do this in D.
May 03 2015
On Sunday, 3 May 2015 at 17:46:54 UTC, Meta wrote:This is not really doable right now in D. You can forward the function calls to a and b easily enough, but you can't inherit from more than one class. Once the multiple alias this patch gets merged you will be able to do this in D.On Sunday, 3 May 2015 at 18:17:26 UTC, Adam D. Ruppe wrote:I'd make class A and class B into mixin templates instead. mixin template A { string a() { return "foo"; } } mixin template B { string b() { return "bar"; } } class C { mixin A; mixin B; } If you still need class A and class B, just make a class that mixes in the template for them too. Since the C++ methods aren't virtual, I imagine you don't really need a base class for them, but if you do want a virtual base class, make interfaces and inherit from as many of them as you need.Thanks. On Sunday, 3 May 2015 at 20:03:00 UTC, QAston wrote:If you want to learn a language properly, translating the idioms directly from what you already know is a bad approach. You're going to be frustrated that something was easy (to you) in your old language and the new one is weird and different than the old one. Also - results are often suboptimal. I've seen this happen too many times to not warn you, but if you just want to carry over and you don't care about learning or quality of result then please carry on.At the moment, I do not do a complete study of the D, so the quality is not important to me. Start a complete study of D, I plan a little later.
May 03 2015
I'd make class A and class B into mixin templates instead. mixin template A { string a() { return "foo"; } } mixin template B { string b() { return "bar"; } } class C { mixin A; mixin B; } If you still need class A and class B, just make a class that mixes in the template for them too. Since the C++ methods aren't virtual, I imagine you don't really need a base class for them, but if you do want a virtual base class, make interfaces and inherit from as many of them as you need.
May 03 2015
On Sunday, 3 May 2015 at 17:35:42 UTC, Dennis Ritchie wrote:Hi, How can I rewrite this code to the D? ----- #include <string> #include <iostream> class A { public: std::string a() { return std::string("foo"); } }; class B { public: std::string b(){ return std::string("bar"); } }; class C : public A, public B {}; int main () { C c; std::cout << c.a() << c.b() << std::endl; return 0; }If you want to learn a language properly, translating the idioms directly from what you already know is a bad approach. You're going to be frustrated that something was easy (to you) in your old language and the new one is weird and different than the old one. Also - results are often suboptimal. I've seen this happen too many times to not warn you, but if you just want to carry over and you don't care about learning or quality of result then please carry on.
May 03 2015