开发者

Calling a method of a constant object parameter

开发者 https://www.devze.com 2022-12-25 20:41 出处:网络
Here is my code that fails: bool Tab开发者_如何转开发le::win(const Card &card) { for (int i = 0; i < cards.size(); i++)

Here is my code that fails:

bool Tab开发者_如何转开发le::win(const Card &card) {
   for (int i = 0; i < cards.size(); i++)
      if (card.getRank() == cards[i].getRank()) return true;

   return false;
}

Error message is: passing 'const Card' as 'this' argument of 'int Card::getRank()' discards qualifiers.

When I get a copy of the card and change the code to this it works:

bool Table::win(const Card &card) {
   Card copyCard = card;

   for (int i = 0; i < cards.size(); i++)
      if (copyCard.getRank() == cards[i].getRank()) return true;

   return false;
}

Is there any other way to do this?


Is getRank a const-method? It should be declared like this":

int getRank( ) const;

Assuming the return type is int.

0

精彩评论

暂无评论...
验证码 换一张
取 消