Cannot get this C++ example migrated to D

Skippy Skippy at gmail.com
Sun Apr 16 05:58:39 UTC 2023


Anyone wanna try converting this C++ example to D?

(I tried, but getting nowhere.. so far).

// --- C++ example - working -----
#include <iostream>
using std::cout;

class test
{
   private:
     int objNo;
     static int objCnt;

   public:
     test()
     {
         objNo = ++objCnt;
     }

     ~test()
     {
         --objCnt;
     }

     void printObjNumber(void)
     {
         cout << "object number :" << objNo << "\n";
     }

     static void printObjCount(void)
     {
         cout << "count:" << objCnt << "\n";
     }
};

int test::objCnt;

int main()
{
     test t1, t2;
     test::printObjCount(); // 2

     test t3;
     test::printObjCount(); // 3

     t1.printObjNumber(); // object number :1
     t2.printObjNumber(); // object number :2
     t3.printObjNumber(); // object number :3

     return 0;
}
// -----------------------

// D code .. not working yet --
module example;
import std:writefln;

class test
{
   private:
     int objNo;
     static int objCnt;

   public:
     this()
     {
         objNo = ++objCnt;
     }

     ~this()
     {
         --objCnt;
     }

     void printObjNumber()
     {
         writefln("object number : %s", objNo);
     }

     static void printObjCount()
     {
         writefln("count: %s", objCnt);
     }
}

// ??
int counter;

// ??
static this()
{
     counter = test.objCnt;
}

int main()
{
     test t1, t2 = new test();

     test.printObjCount();

     test t3;
     test.printObjCount();

     t1.printObjNumber();
     t2.printObjNumber();
     t3.printObjNumber();

     return 0;
}
// --------------------



More information about the Digitalmars-d-learn mailing list