www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - question regarding oop

reply Moritz <mpipahl gspgmbh.com> writes:
Hello everyone,

Im new to the D language and have a question regarding a slightly 
advanced matter:

Im currently porting the code of a game Im writing to D, and right now 
Im working on the event handling (reacting to user input).
Since Im doing a strategy game, I have several menues and I need a way 
to connect a menu to the state my game is in, so when the player clicks 
somewhere, I can use the hashtable to determine which menu class should 
react.

I think an asossiative array is the way I should go, key would be the 
state my game is in and the value would be the corresponding Menu.
But since there are several different menu classes, I need some way to 
put them all in the value area of the array.

In C++, I could make them all inherit from a specific base class, and 
use the base class as data type for the value, so I effectively had 
different menues in one datatype in that hashmap.

How can I do this in D?
Jun 28 2008
next sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
Moritz <mpipahl gspgmbh.com> wrote:

 Hello everyone,

 Im new to the D language and have a question regarding a slightly  
 advanced matter:

 Im currently porting the code of a game Im writing to D, and right now  
 Im working on the event handling (reacting to user input).
 Since Im doing a strategy game, I have several menues and I need a way  
 to connect a menu to the state my game is in, so when the player clicks  
 somewhere, I can use the hashtable to determine which menu class should  
 react.

 I think an asossiative array is the way I should go, key would be the  
 state my game is in and the value would be the corresponding Menu.
 But since there are several different menu classes, I need some way to  
 put them all in the value area of the array.

 In C++, I could make them all inherit from a specific base class, and  
 use the base class as data type for the value, so I effectively had  
 different menues in one datatype in that hashmap.

 How can I do this in D?

The exact same way, for one. class Menu { } class DerivedMenu : Menu { } Menu[GameState] menuArray; ... menuArray[currentGameState] = new DerivedMenu(); -- Simen
Jun 28 2008
parent Moritz <mpipahl gspgmbh.com> writes:
Thanks a lot to both of you :)

Simen Kjaeraas wrote:
 The exact same way, for one.
 
 
 class Menu
 {
 }
 
 class DerivedMenu : Menu
 {
 }
 
 Menu[GameState] menuArray;
 
 ...
 
 menuArray[currentGameState] = new DerivedMenu();
 
 -- Simen

Jason House wrote:
 Yes.  Classes can inherit from another class.

 class base{}
 class menuA : public base{}

 D allows only one base class, but many interfaces.

Jun 29 2008
prev sibling parent Jason House <jason.james.house gmail.com> writes:
Moritz wrote:

 Hello everyone,
 
 Im new to the D language and have a question regarding a slightly
 advanced matter:
 
 Im currently porting the code of a game Im writing to D, and right now
 Im working on the event handling (reacting to user input).
 Since Im doing a strategy game, I have several menues and I need a way
 to connect a menu to the state my game is in, so when the player clicks
 somewhere, I can use the hashtable to determine which menu class should
 react.
 
 I think an asossiative array is the way I should go, key would be the
 state my game is in and the value would be the corresponding Menu.
 But since there are several different menu classes, I need some way to
 put them all in the value area of the array.
 
 In C++, I could make them all inherit from a specific base class, and
 use the base class as data type for the value, so I effectively had
 different menues in one datatype in that hashmap.
 
 How can I do this in D?

Yes. Classes can inherit from another class. class base{} class menuA : public base{} D allows only one base class, but many interfaces.
Jun 28 2008