IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

Convertir une énumération Java en class C++


Sujet :

C++

  1. #1
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2013
    Messages
    1
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Décembre 2013
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Convertir une énumération Java en class C++
    Bonjour à tous,
    J'ai écrit une énumération en Java que j'aimerais traduire en C++ dans le cadre d'un projet mais je n'arrive pas à trouver de solution pour "traduire" du Java au C++. Des conseils ? Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
     
    public enum Move {
     
    	UP(0, -1), DOWN(0, 1), RIGHT(1, 0), LEFT(-1, 0);
     
    	private int horizontalMove;
    	private int verticalMove;
     
    	private Move(int horizontal, int vertical) {
    		this.horizontalMove = horizontal;
    		this.verticalMove = vertical;
    	}
     
    	public int getHorizontalMove() {
    		return horizontalMove;
    	}
     
    	public int getVerticalMove() {
    		return verticalMove;
    	}
     
    	public CellLocation getNextCellLocation(CellLocation currentLocation) {
    		return new CellLocation(getNextRow(currentLocation.getRowIndex()),
    				getNextColumn(currentLocation.getColumnIndex()));
    	}
     
    	private int getNextRow(int currentRow) {
    		return currentRow + verticalMove;
    	}
     
    	private int getNextColumn(int currentColumn) {
    		return currentColumn + horizontalMove;
    	}
     
    	public Move getInverse() {
    		switch (this) {
    		case UP:
    			return DOWN;
    		case DOWN:
    			return UP;
    		case LEFT:
    			return RIGHT;
    		case RIGHT:
    			return LEFT;
     
    		}
    		return null;
    	}
    }
    Comme vous pouvez le voir, il s'agit d'une énumération qui me permet de me déplacer dans un tableau 2D.
    Merci d'avance à tous et à toutes.

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Analyste/ Programmeur
    Inscrit en
    Juillet 2013
    Messages
    4 630
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Analyste/ Programmeur

    Informations forums :
    Inscription : Juillet 2013
    Messages : 4 630
    Points : 10 556
    Points
    10 556
    Par défaut
    Moi je ferais un patron "État - State" en semi-objet [et pas besoin de pointeurs de fonction dans ton cas ]

    PS: je supprime la méthode getNextCellLocation.

    Code c++ : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    class Move {
    // Or maybe typedef struct s_Move {
    //  Constructors
    //  public:
    //   s_Move(...) ... { ... }
    //
    // ...
    // } Move;
     
    // Constructors
    public:
        Move() : horizontal_move(0), vertical_move(0), inverse(MOVE_DEFAULT) {}
     
    //  Or
    //  Move() { clean(); }
     
        Move(int init_horizontal_move, int init_vertical_move, unsigned char init_inverse) : horizontal_move(init_horizontal_move), vertical_move(init_vertical_move) {
            inverse = ((init_inverse < NB_MOVES)? init_inverse: MOVE_NOT_INIT /*or MOVE_DEFAULT or MOVE_ERROR*/  );
        }
     
    //  Or
    //  Move(int init_horizontal_move, int init_vertical_move, unsigned char init_inverse) { init(init_horizontal_move, init_vertical_move, init_inverse); }
     
        Move(const Move& other) { horizontal_move =  other.horizontal_move; vertical_move = other.vertical_move; }
     
     
    // Setters-Getters only if attributes are either private or protected
    public:
     
        int get_horizontal_move() { return horizontal_move; }
     
        int get_inverse()  { return inverse; }
     
        int get_vertical_move() { return vertical_move; }
     
        void set_inverse(unsigned char new_inverse) { if (new_inverse < NB_MOVES) { inverse = new_inverse; }}
     
     
    // Public Interface
    public:
     
    //  XXX Warning: the method init checks if this is a move: so we cannot pass MOVE_NOT_INIT or MOVE_ERROR
        void clean() { init(0, 0, MOVE_DEFAULT); }
     
        int get_next_column(int current_row) { return (current_row + horizontal_move); }
     
        int get_next_row(int current_row) { return (current_row + vertical_move); }
     
        void init(int init_horizontal_move, int init_vertical_move, unsigned char init_inverse) {
            horizontal_move = init_horizontal_move;
            vertical_move = init_vertical_move;
            inverse = ((init_inverse < NB_MOVES)? init_inverse: MOVE_NOT_INIT /*or MOVE_DEFAULT or MOVE_ERROR*/ );
        }
     
     
    // Attributes
    public: // or private: or protected:
     
        int horizontal_move;
        int vertical_move;
        unsigned char inverse;
    };


    Code c : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    /*typedef*/ enum e_MOVE_NAME : unsigned char {
        MOVE_UP = 0,
        MOVE_DOWN,
        MOVE_RIGHT,
        MOVE_LEFT,
        NB_MOVES,
        MOVE_NOT_INIT,
        /*MOVE_ERROR,*/
        MOVE_DEFAULT = MOVE_UP // or MOVE_NOT_INIT but always the last in enum
    } /*MOVE_NAME*/;


    Et plus loin, mais on peut le faire avec un tableau dynamique et avec des pointeurs intelligents [ou sûrement sans]: tout est possible :
    Code c++ : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
        Move* tab_moves[NB_MOVES];
     
        Move* tmp = new Move(0, -1, MOVE_DOWN):
        tab_moves[MOVE_UP] = tmp;
     
        tmp = new Move(0, 1, MOVE_UP):
        tab_moves[MOVE_DOWN] = tmp;
     
        tmp = new Move(1, 0, MOVE_LEFT):
        tab_moves[MOVE_RIGHT] = tmp;
     
        tmp = new Move(-1, 0, MOVE_RIGHT):
        tab_moves[MOVE_LEFT] = tmp;
     
     
    ....
     
    //  OnClose()
        for(unsigned char move = 0; move < NB_MOVES; ++move) { delete tab_moves[move]; /*tab_moves[move] = NULL;*/ }

    Édit 1: Je ne peux pas tester mais je me demande si on ne peut pas faire un code plus liant comme

    Code c++ : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
        Move* tab_moves[NB_MOVES] = {
            [MOVE_UP]    = new Move( 0, -1, MOVE_DOWN),
            [MOVE_DOWN]  = new Move( 0,  1, MOVE_UP),
            [MOVE_RIGHT] = new Move( 1,  0, MOVE_LEFT),
            [MOVE_LEFT]  = new Move(-1,  0, MOVE_RIGHT)
        };

    Édit 2:
    • La classe Move peut être une structure si on n'a pas de méthode virtuelle ni d'héritage. Et encore plus si un seul objet manipule le tableau des mouvements en mettant tous les attributs en public.
    • Il fait comprendre que les valeurs MOVE_NAME sont des index de ton tableau de mouvements
    • On peut créer des valeurs qui ne sont pas des mouvements, comme par exemple MOVE_ERROR ou MOVE_NOT_INIT, en les plaçant après NB_MOVES. Un mouvement passera le test move < NB_MOVES.
    • Avec un tableau de mouvements statiques Move tab_moves[NB_MOVES], il faut un constructeur par défaut vide (demandé lors de la construction de ton tableau) et ensuite une méthode init (ou un autre nom) pour initialiser ton mouvement



    Édit 3: g++ me fait un warning "scoped enums only available with -std=c++11 or -std=gnu++11 [enabled by default]".
    Pourtant avec le vieux compilateur C++ de C++ Bulder xe cela passe sans problème

Discussions similaires

  1. Convertir une scriptlet JAVA via des tags JSP struts
    Par seb0634 dans le forum Servlets/JSP
    Réponses: 5
    Dernier message: 16/09/2008, 15h00
  2. python C API: convertir une struct C en Class python
    Par dmichel dans le forum Interfaçage autre langage
    Réponses: 11
    Dernier message: 25/06/2008, 16h30
  3. Convertir une application Java en applet ?
    Par Nitroman_70 dans le forum Applets
    Réponses: 4
    Dernier message: 04/06/2008, 09h52
  4. [APPLET] convertire une application JAVA en applet
    Par wickramben dans le forum Applets
    Réponses: 1
    Dernier message: 13/04/2006, 10h01
  5. Convertir une classe en .HPP
    Par kmaniche dans le forum C++Builder
    Réponses: 10
    Dernier message: 09/02/2006, 13h20

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo