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
    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 confirm�
    Homme Profil pro
    Analyste/ Programmeur
    Inscrit en
    Juillet 2013
    Messages
    4 772
    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 772
    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