简体   繁体   English

在重载运算符时使用函数

[英]Using functions while overloading an operator

I'm currently working on a game to brush up my C++ skills and I'm trying to use more advanced concepts in order to learn more about the language itself.我目前正在开发一款游戏来提升我的 C++ 技能,并且我正在尝试使用更高级的概念来了解更多关于语言本身的信息。

I'm creating a deck of cards using an enum to store the values and would like to overload the << operator to have a clean way to print everything.我正在使用enum创建一副卡片来存储值,并希望重载<<运算符以有一种干净的方式来打印所有内容。

//Card.h
private:
    Value value;
    Suit suit;

enum Value {
        TWO = 2, THREE...
    };
enum Suit {
        DIAMONDS, SPADES, CLUBS, HEARTS
    };
Card(Value value, Suit suit);
friend std::ostream& operator<<(std::ostream& output, const Card& card);

In order to maintain readability, I have get functions for values and suits using switch/case.为了保持可读性,我使用 switch/case get值和套装的功能。

std::string Card::getSuit() {
    int c = suit;
    std::string strSuit;
    switch(c) {
        case 0:
            strSuit = "Diamonds";
            break;
        case 1:
            strSuit = "Spades";
            break;
        case 2:
            strSuit = "Clubs";
            break;
        case 3:
            strSuit = "Hearts";
            break;
        default:
            ;
    }
    return strSuit;
}

( getValue is similar but omitted to reduce question length) getValue类似但省略以减少问题长度)

My idea for the function to overload the operator is:我对 function 使运算符重载的想法是:

std::ostream& operator<<(std::ostream& output, const Card& card) {
    std::string v = card.getValue();
    std::string s = card.getSuit();
    output << v << " of " << s << std::endl;
    return output;
}

however the get functions do not work because of a conflict with the const (which I do not fully understand).但是由于与const冲突(我不完全理解), get函数不起作用。

How can I overload this operator cleanly, without having to duplicate the switch/case statements?我怎样才能干净地重载这个运算符,而不必复制 switch/case 语句?

'const' on the C++ member function basically means that it's 'read-only',the function is not allowed to alter/modify any member variables of the class-instance (this) - and to to call other functions, those must also be 'const', example, this would be illegal, and not compile: 'const' on the C++ member function basically means that it's 'read-only',the function is not allowed to alter/modify any member variables of the class-instance (this) - and to to call other functions, those must also be 'const',例如,这将是非法的,并且无法编译:

int SomeClass::getSomeValue() const
{
    this->some_example_counter++; // not allowed to modify member here
    return this->some_member_value;
}

Now it's possible to define 'some_example_counter' as 'mutable', and then it will be allowed to be modified/altered from a 'const' member function.现在可以将“some_example_counter”定义为“可变”,然后将允许从“const”成员 function 修改/更改它。

class SomeClass {
   ...
   mutable int some_example_counter;
}

There are some few rare use-cases where 'mutable' will make sense, like for example when a 'mutex' may be needed, otherwise personally I avoid using 'mutable'.有一些罕见的用例“可变”会有意义,例如当可能需要“互斥锁”时,否则我个人避免使用“可变”。

In your code:在您的代码中:

std::ostream& operator<<(std::ostream& output, const Card& card)

The second parameter 'const Card& card' - you can only call memeber function of Card class that are 'const' (read-only) I think it would in any case make sense to have both Card::getValue() and Card::getSuit() as 'const' - as they naturally don't alter/modify any member variable of Card.第二个参数 'const Card& card' - 您只能调用 Card class 的成员 function 是 'const' (只读)我认为在任何情况下同时拥有 Card::getValue() 和 Card: 都是有意义的: getSuit() as 'const' - 因为它们自然不会更改/修改 Card 的任何成员变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM