Translate C++ to D

doob doobnet at gmail.com
Thu Jan 10 13:01:03 PST 2008


Luca Lupo wrote:
> I would like to thank all the answers, you are very available. Now I have to start converting the files. Cpp and I hope that you can continue to help.
> 
> This is the first file: observer.cpp, any suggestions?
> 
> //Observer.cpp
> 
> #include<iostream>
> #include<string> 
> #include"Subject.h" 
> using namespace std;
> 
> Observer::~Observer()
> {}
> 
> Observer::Observer()
> {}
> 
> SubscriberG::SubscriberG(string a){
>      indirizzo_e_mail_G = a;
> }
> 
> SubscriberS::SubscriberS(string a,string b){
>      indirizzo_e_mail_S = a;
>      name_author = b;
> }
> 
> 
> void Observer::Update(Subject* b)
> {}
> 
> void Subscriber::Update(Subject* b)
> {}
> 
> void SubscriberS::Update(Subject* b){
>      if (name_author == ((Blog *)b)->GetAuthor()){
>         cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl;
>         ((Blog *)b)->GetTitle();
>         ((Blog *)b)->GetText();
>         cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
>         cout<<endl<<endl;
>      }
> }
> 
> void SubscriberG::Update(Subject* b){
>      cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl;
>      ((Blog *)b)->GetTitle();
>      ((Blog *)b)->GetLine();
>      cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
>      cout<<endl<<endl;
> }
> 
> 
> 
> //Observer.d
> 
> import std.string;
> import Subject
> 
> Observer::~this() //distructor for observer
> {}
> 
> Observer::this() // costructor for observer
> {}
> 
> SubscriberG::this(string a) //contructor for subscriberG
> {indirizzo_e_mail_G = a;}
> 
> SubscriberS::this(string a,string b) //contructor for subscriberS
> {
>     indirizzo_e_mail_S = a;
>     name_author = b;
> }
> 
> 
> void Observer::Update(Subject* b) //it's equal? for me yes!
> {}
> 
> void Subscriber::Update(Subject* b) //it's equal? for me yes!
> {}
> 
> void SubscriberS::Update(Subject* b) //Help??
> {
>     if(name_author == ((Blog *)b)->GetAuthor()){
> cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl;
> ((Blog *)b)->GetTitle();
> ((Blog *)b)->GetText();
> cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
>                                                }
> cout<<endl<<endl;
> }
> 
> void SubscriberG::Update(Subject* b) //Help??
> {
> cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl;
> ((Blog *)b)->GetTitle();
> ((Blog *)b)->GetLine();
> cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
> cout<<endl<<endl;
> }
> 

I think the best would be if read through the language specification 
(http://www.digitalmars.com/d/1.0/lex.html) and tried to port files own 
your own and then ask if it's something you don't understand or need 
help with.

Some corrections and tips:
Drop all the name spaces
All member functions/methods must be in the class not as free functions. 
Think of it as you write a class declaration in a header file but you 
also add the implementation.
Classes are by reference in D so drop all the pointers
":: -> ." are all written as "." in D
If any class has a pure virtual function make it an abstract function 
(put the keyword "abstract" in front of the function and drop "=0") or 
if all functions are pure functions then make the class an interface 
instead(replace the "class" keyword with the "interface" keyword)
Don't put a semicolon after a class

Tips: put every class in it's own module/file




More information about the Digitalmars-d mailing list