www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Classes. C++ to D

reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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
next sibling parent reply "Meta" <jared771 gmail.com> writes:
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
parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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
prev sibling next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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
prev sibling parent "QAston" <qastonx gmail.com> writes:
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