r/learnjava • • Jun 01 '21

Help translating from c++ to java

Hey, could you please help me translation this code to java?

class DialogDirector {
public:
virtual ~DialogDirector();
virtual void ShowDialog();
virtual void WidgetChanged(Widget*) = 0;
protected:
DialogDirector();
virtual void CreateWidgets() = 0;
};

additionally what does -> and :: mean in c++, and how could i translate it to java?

0 Upvotes

8 comments sorted by

View all comments

3

u/Aenno Jun 01 '21

Adding to what u/loudsight said, -> in C++ is used as . in Java, however for pointers.

And :: is used to inherit from a superclass, like A::B, where B extends A (in Java). Double colon is used to implement methods from a .h file as well. Such as: foo(); can be declared in the .h file and when you define it in the .cpp file you simply say Entity::foo (assuming the name of the class is Entity)

1

u/Shuffleto Jun 01 '21 edited Jun 01 '21

thank you for your Response! Could you please help me with this

class Widget {
public: Widget(DialogDirector*); 
virtual void Changed(); 
virtual void HandleMouse(MouseEvent& event); 
// ... 
private: DialogDirector* _director; }; 
void Widget::Changed () { _director->WidgetChanged(this); }

im not understanding this part

void Widget::Changed () {
_director->WidgetChanged(this); }

how would it look like in java?

void Changed(){
    _director.widgetChanged(this);
}

like this?

2

u/Aenno Jun 01 '21

Seems about right. It should work as long as Changed() is defined in Widget's scope.

1

u/Shuffleto Jun 01 '21

could you please explain how could i put it into the class? Should i just change the name and give it public modifier?

1

u/Aenno Jun 01 '21

Just define it in the class you want to work with, in your case "Widget", like you normally would btw, no special mumbo jumbo