2 years ago

#57873

test-img

KeyboardClone

Am I Incorrectly Writing my Copy Assignment Operators?

I've been working on a personal project where I code a simple Blackjack game, nothing too fancy. However, in an attempt to make copy assignment operators for my Card class, my CLion linter is suggesting that how I am writing them is incorrect and is suggesting an alternate method that I've never seen before.

Here is the header declaration for the Card class, with the main focus being on the copy assignment operator (exists within Card.h):

class Card
{
    private:
        int cardIndex; // each value from 0-52 represents a particular playing card,
                       // with 0 representing the back of a card
    public:
        Card();
        Card(int cardIndex);
        Card(const Card &otherCard);
        Card& operator=(const Card &otherCard);
        Card(Card &&otherCard) noexcept;             // undefined move constructor
        Card& operator=(Card &&otherCard) noexcept;  // undefined move assignment operator
        ~Card();
        int getIndex();
        void setIndex(int cardIndex);
        void display();
};

Here is how I tried to define it, with the CLion linter notably not color coding the class's data member that I know is declared (exists within Card.cpp):

&Card Card::operator=(const Card &otherCard)
{
    this->cardIndex = otherCard.cardIndex;
    return *this;
}

Here is what the CLion linter tried to autocomplete for me, which brings back the the color coding for the cardIndex data member as though it's able to recognize it exists only in this state:

&Card::operator=(const Card &otherCard)
{
    this->cardIndex = otherCard.cardIndex;
    return *this;
}

Correct me if I'm wrong but this should not be correct whatsoever, unless there's some syntactical reason that my code would be compiled correctly with this method signature I am yet unaware of. I highly doubt this is the case and that my linter is futzing something somewhere or I've made some grave mistake with my class declaration, but this wouldn't be the first time that CLion has revealed some weird thing to be more correct/be possible in the first place.

c++

syntax

clion

linter

copy-assignment

0 Answers

Your Answer

Accepted video resources