如何告诉编译器使用重载运算符而不是使用“普通”运算符?

时间:2016-11-15 18:39:43

标签: c++ enums operator-overloading

这里我有枚举类:

enum class wahl {
   schere  , stein  , papier 
};

然后我重载运算符<和>

bool operator<(wahl &wahl1, wahl &wahl2) {
  switch (wahl1) {
    case wahl::papier: {
        if (wahl2 == wahl::schere) {
            return true; break;
        } //papier < schere
        else if (wahl2 == wahl::stein) {
            return false; break;
        } //papier > stein
        else {
            return false; break;
        }
    }
    case wahl::schere: {
        if (wahl2 == wahl::stein) {
            return true; break;
        } //schere < stein
        else if (wahl2 == wahl::papier) {
            return false; break;
        } //schere > papier
        else {
            return false; break;
        }
    }
    case wahl::stein: {
        if (wahl2 == wahl::papier) {
            return true; break;
        } //stein < papier
        else if (wahl2 == wahl::schere) {
            return false; break;
        } //stein > schere
        else {
            return false; break;
        }
     }
  }
};

bool operator > (const wahl wahl1, const wahl wahl2) {
switch (wahl1) {
case wahl::papier: {
    if (wahl2 == wahl::schere) {
        return false; break;
    }
    // papier < schere
    else if (wahl2 == wahl::stein) {
        return true; break;
    } //papier > stein
    else {
        return false; break;
    }
}
case wahl::schere: {
    if (wahl2 == wahl::stein) {
        return false; break;
    } //schere < stein
    else if (wahl2 == wahl::papier) {
        return true; break;
    } //schere > papier
    else {
        return false; break;
    }
}
case wahl::stein: {
    if (wahl2 == wahl::papier) {
        return false; break;
    } //stein < papier
    else if (wahl2 == wahl::schere) {
        return true; break;
    } //stein > schere
    else {
        return false; break;

    }
}
}
};

我有另一个名为player的课程:

class player {
wahl pl_wahl;
int pl_score;
char* pl_name;

public:

player() {}
player(int score, wahl wahl, char* name) : 
    pl_wahl{ wahl }, pl_score{ score }, pl_name{ name } {}

wahl pl_get_wahl() {
    return pl_wahl;
}

char* pl_get_name() {
    return pl_name;
}

int &pl_get_score() {
    return pl_score;
}
};

在这里我使用了比较器:

class game {

player game_player1, game_player2, game_momentan_gewinner;
int game_score_max;

public:
game() {}
game(player player1, player player2, int score_max) : 
    game_player1{player1},
    game_player2{player2},
    game_score_max{ score_max } {}

void vergleichen() {
    if (game_player1.pl_get_wahl() > game_player2.pl_get_wahl()) {
        game_momentan_gewinner = game_player1;
        std::cout << "Gewinner dieser Runde ist Player 1 : " <<
            game_momentan_gewinner.pl_get_name() << std::endl;
    }
    if (game_player1.pl_get_wahl() < game_player2.pl_get_wahl()) {
        game_momentan_gewinner = game_player2;
        std::cout << "Gewinner dieser Runde ist Player 2 : " <<
            game_momentan_gewinner.pl_get_name() << std::endl;
    }
    else {
        std::cout << "Remis" << std::endl;
    }

}
};

我遇到的问题是enum将以int身份继续进行。如果我问比较两个枚举变量,结果将取决于int值而不是取值,我用重载运算符设置。

有没有办法,我可以阻止编译器使用枚举变量的int值,并在重载运算符中以我想要的方式比较枚举变量?

1 个答案:

答案 0 :(得分:0)

如果使用正确的签名,则应该没问题:

c1 = 0;
M = bresenham_line([1 1 Nx/2+1 Ny+1]);
medium.sound_speed = c0*ones(Nx,Ny) - (c0*M) + c1*M; 

Demo