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 :

Typages dynamiques à l'exécution


Sujet :

C++

  1. #21
    Membre confirmé
    Profil pro
    Inscrit en
    Août 2004
    Messages
    152
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Août 2004
    Messages : 152
    Par défaut
    Citation Envoyé par b Oo Voir le message
    La classe ValueCreator est un singleton et j'ai mis ?? pour le type de current car je ne sais pas à quoi il correspond.
    Merci bien, je verrais car là j'ai un petit problème pour les visiteurs. Il semblerait qu'il ne détecte pas les bon types ! Dans mes visiteurs pour faire upgrader/downgrader les caractéristiques c'est ma fonction avec deux templates de type différents qui match et pourtant typeid().name() me retourne le même type et le bon pourtant !

    les visiteurs :
    Code CPP : 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
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    class charactPlusOperator : public boost::static_visitor<void> {
    	public:
    		// handle each types
    		void operator()(unsigned int &x, unsigned int &y) const {
    			x = (x + y);
    			cout << "    Uint: "<<x<<" + "<<y<<" !";
    		}
    		void operator()(signed int &x, signed int &y) const {
    			x = (x + y);
    			cout << "    Sint: "<<x<<" + "<<y<<" !";
    		}
    		void operator()(bool &x, bool &y) const {
    			x = (x & y);
    			cout << "    bool: "<<x<<" & "<<y<<" !";
    		}
    		void operator()(string &x, string &y) const {
    			x = (x + y);
    			cout << "    string: "<<x<<" + "<<y<<" !";
    		}
    		void operator()(float &x, float &y) const {
    			x = (x + y);
    			cout << "    float: "<<x<<" + "<<y<<" !";
    		}
    		template <typename T> void operator()(T &x, T &y) const {
    			x = y;
    			cout << "    same Template: "<<x<<" = "<<y<<" !";
    		}
    		template <typename T, typename U> void operator()(T &x, U &y) const {
    			x = x;
    			cout << "    error types mismatch ("<< typeid(T).name() << " vs "<<typeid(U).name()<<") !";
    		}
    		template <typename T> void operator()(T &x) const {
    			x = x;
    			cout << "    error one argv given !";
    		}
    };
     
    class charactMinusOperator : public boost::static_visitor<void> {
    	public:
    		// handle each types
    		void operator()(unsigned int &x, unsigned int &y) const {
    			x = (x - y);
    			cout << " U["<<x<<"] ";
    		}
    		void operator()(signed int &x, signed int &y) const {
    			x = (x - y);
    			cout << " S["<<x<<"] ";
    		}
    		void operator()(bool &x, bool &y) const {
    			x = (x & y);
    			cout << " B["<<x<<"] ";
    		}
    		void operator()(string &x, string &y) const {
    			x = y;
    			cout << " St["<<x<<"] ";
    		}
    		void operator()(float &x, float &y) const {
    			x = (x - y);
    			cout << " F["<<x<<"] ";
    		}
    		template <typename T> void operator()(T &x, T &y) const {
    			x = y;
    			cout << " ?["<<x<<"] ";
    		}
    		template <typename T, typename U> void operator()(T &x, U &y) const {
    			x = x;
    			cout << "    error types mismatch ("<< typeid(T).name() << " vs "<<typeid(U).name()<<") !";
    		}
    		template <typename T> void operator()(T &x) const {
    			x = x;
    			cout << "    error one argv given !";
    		}
    };
     
    class charactZeroOperator : public boost::static_visitor<void> {
    	public:
    		// handle each types
    		void operator()(unsigned int &x, unsigned int &y) const {
    			x = y;
    			cout << " U["<<x<<"] ";
    		}
    		void operator()(signed int &x, signed int &y) const {
    			x = y;
    			cout << " S["<<x<<"] ";
    		}
    		void operator()(bool &x, bool &y) const {
    			x = y;
    			cout << " B["<<x<<"] ";
    		}
    		void operator()(string &x, string &y) const {
    			x = y;
    			cout << " St["<<x<<"] ";
    		}
    		void operator()(float &x, float &y) const {
    			x = y;
    			cout << " F["<<x<<"] ";
    		}
    		template <typename T> void operator()(T &x, T &y) const {
    			x = y;
    			cout << " ?["<<x<<"] ";
    		}
    		template <typename T, typename U> void operator()(T &x, U &y) const {
    			x = x;
    			cout << "    error types mismatch ("<< typeid(T).name() << " vs "<<typeid(U).name()<<") !";
    		}
    		template <typename T> void operator()(T &x) const {
    			x = x;
    			cout << "    error one argv given !";
    		}
    };

    Méthode de Characteristic appelée pour faire l'upgrade
    Code CPP : 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
    Characteristic Characteristic::doOperator(string _currOp, const Characteristic &upgrader) {
    	if (_currOp.empty())
    		return *this;
     
    	cout << "  Before: "<< value << "+"<< upgrader.value;
    	if (_currOp == string("+")) {
    		cout << " (+) ";
    		boost::apply_visitor(charactPlusOperator(), value, upgrader.value);
    	}
    	else if (_currOp == string("-")) {
    		cout << " (-) ";
    		boost::apply_visitor(charactMinusOperator(), value, upgrader.value);
    	}
    	else if (_currOp == string("0")) {
    		cout << " (0) ";
    		boost::apply_visitor(charactZeroOperator(), value, upgrader.value);
    	}
    	else if (_currOp == string("-1")) {
    		cout << " (0) ";
    		// Do nothing
    	}
    	else {
    		cout << " (/) ";
    		// Do nothing
    	}
    	cout << " ; After: " << value << endl;
     
    	return *this;
    }

    L'output intéressant du programme :
    Code CPP : 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
    /*
    doUpgrade() called
     aoeRangeCases addition:
      Before: 4+0 (+)     error types mismatch (j vs j) ! ; After: 4
     attackRate addition:
      Before: 5+0 (-)     error types mismatch (j vs j) ! ; After: 5
     attackType addition:
      Before: 3+3 (0)     error types mismatch (10damageType vs 10damageType) ! ; After: 3
     canMove addition:
      Before: 0+0 (0)     error types mismatch (b vs b) ! ; After: 0
     casesHeight addition:
      Before: 1+1 (0)     error types mismatch (j vs j) ! ; After: 1
     casesWidth addition:
      Before: 1+1 (0)     error types mismatch (j vs j) ! ; After: 1
     firePower addition:
      Before: 5+10 (+)     error types mismatch (j vs j) ! ; After: 5
     fireRangeCases addition:
      Before: 3+0 (+)     error types mismatch (j vs j) ! ; After: 3
     maxHp addition:
      Before: 35+5 (+)     error types mismatch (j vs j) ! ; After: 35
     moveRate addition:
      Before: 0+0 (-)     error types mismatch (j vs j) ! ; After: 0
     runnerFlags addition:
      Before: 1+1 (0)     error types mismatch (17entityRunnerFlags vs 17entityRunnerFlags) ! ; After: 1
     texture addition:
      Before: texture.xml+texture.xml (0)     error types mismatch (Ss vs Ss) ! ; After: texture.xml
     tower addition:
      Before: 1+1 (0)     error types mismatch (b vs b) ! ; After: 1
     towerFlags addition:
      Before: 8+8 (0)     error types mismatch (16entityTowerFlags vs 16entityTowerFlags) ! ; After: 8
     upgrade addition:
      Before: 0+1 (0)  ; After: 0
    Upgrade complete (0)
    */

    Puisque tout semble tomber dans deux types différents il forcément impossible d'affecter x avec y, le compilateur me renvoie des erreurs bien sûr ! Peut-être avez-vous une idée de pourquoi il fait l'autruche ? Car tout le reste du programme fonctionne très bien, les types sont correctes, on le voit bien avec typeid(T / U).name() c'est les mêmes !

    Merci d'avance.

  2. #22
    Membre confirmé Avatar de b Oo
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    179
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 179
    Par défaut
    Salut,
    cela ne fonctionne pas car tu passes des paramètres non const à tes méthodes.
    Il faut que tu fasses :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    class charactPlusOperator : public boost::static_visitor<void> {
    	public:
    		// handle each types
    		void operator()(unsigned int &x, const /* CONST ICI*/ unsigned int &y) const {
    			x = (x + y);
    			cout << "    Uint: "<<x<<" + "<<y<<" !";
    		}
    ...
    // car tu as déclaré
    Characteristic Characteristic::doOperator(string _currOp, const /* CONST */ Characteristic &upgrader)
    ...
    // et que tu fais bien l'appel dans cet ordre
    boost::apply_visitor(charactPlusOperator(), value, upgrader.value); //upgrader est const

  3. #23
    Membre confirmé
    Profil pro
    Inscrit en
    Août 2004
    Messages
    152
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Août 2004
    Messages : 152
    Par défaut
    Ah bas super ! ça marche maintenant exactement comme je l'avais souhaité. J'aurais jamais pensé au const !!! Je pensais même pas que c'était important là, merci mille fois b Oo

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. VBA, typage dynamique, références
    Par raboliot dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 11/06/2007, 11h24
  2. Réponses: 36
    Dernier message: 09/09/2006, 03h06
  3. id et typage dynamique
    Par Omfraax dans le forum Développement OS X
    Réponses: 5
    Dernier message: 23/08/2006, 19h13
  4. Réponses: 2
    Dernier message: 14/07/2006, 14h24
  5. [Débat] Que pensez-vous des langages à typage dynamique?
    Par Eusebius dans le forum Langages de programmation
    Réponses: 14
    Dernier message: 16/06/2004, 12h12

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