Llu*_*uis 0 c++ class operator-overloading equality-operator
我正在做一种方法来查看两张卡号是否相同
这是我的重载运算符和我称之为重载运算符的方法
bool Carta::operator==(Carta *c){
if(getNumCarta()==c->getNumCarta() || getNumPal()==c->getNumPal()){
return true;
}
else{
return false;
}
//return(numCarta==c->numCarta)||(numPal!==c->numPal);
}
bool Carta::operator!=(Carta *c){
if(getNumCarta()!=c->getNumCarta() && getNumPal()!=c->getNumPal()){
return true;
}
else{
return false;
}
}
bool Table::pairs(){
for(int i=1;i<posFull;i++){
if(t[i-1]->operator == t[i+1]){
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我期望返回一个真实的但永远不会发生
看来你是说
if(t[i-1]->operator ==( t[i+1]) ){
Run Code Online (Sandbox Code Playgroud)
或者你可以写
if( *t[i-1] == t[i+1] ){
Run Code Online (Sandbox Code Playgroud)
但是此运算符声明
bool operator==(Carta *c);
Run Code Online (Sandbox Code Playgroud)
只会使代码的读者感到困惑。最好将运算符声明为
bool operator ==( const Carta &c ) const;
Run Code Online (Sandbox Code Playgroud)
并像这样使用
if( *t[i-1] == *t[i+1] ){
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |