2

So I am making a Chess Engine right now, and I have a Game class containing an Advantage object, and I am using an odd method to modify them. It involves the Game class calling a member function for the Advantage class, and it passes itself by reference to the Advantage class, and is marked as const. I am now getting some errors that I didn't used to, and decided I didn't really understand fully how this was working. So here's some pseudocode, can someone explain it to me?

class Game ;
class Advantage {
private:
    bool whiteWinning_ = true;
public:
    void updateAdvantage(const Game& game);
}
class Game {
private:
    Advantage advantage_;
    int pawnLocation_ = 1; //obviously, this isn't a real chess game, but still...
public:
    void makeMove(int pawnSteps);
    bool getWinning() const;
}

void Advantage::updateAdvantage(const Game& game){
    //Game is marked const, but the same object is changed because advantage_ is being changed???
    whiteWinning_ = game.getWinning();
}

void Game::makeMove(int pawnSteps) {
    pawnLocation_ += pawnsteps;
    advantage_.updateAdvantage(*this);
}

bool Game::getWinning() const {
    return pawnLocation_ >= 8;
}

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Miles C.
  • 111
  • 4
  • 5
    What are the errors? I don't see any problem with the code above. The member function `Advantage::updateAdvantage` is not marked const so there is no issue with `whiteWinning_` being updated. – ChrisD Nov 16 '21 at 06:31
  • instead of chasing hypothetical errors, maybe point out actual.. or try narrow it down. If they are run-time errors, some debugging is in order. – Swift - Friday Pie Nov 16 '21 at 08:47

1 Answers1

1

The game object which is passed to Advantage::updateAdvantage is casting your argument to const. However the Advantage object in Advantage::updateAdvantage which is denoted as this isn't const, thus allowing you writing to whiteWinning_.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
kobi
  • 101
  • 7
  • Re: "casting your argument" -- there are no casts in this code. At the point of the all, the name of the object is **converted** to a `const` reference. A cast is something you write in your source code to tell the compiler to do a conversion. +1. – Pete Becker Nov 16 '21 at 14:09
  • You are defintly wrong. casting might be something you see in code in case of explicit cast, but you might have a cast which bot appear in code its called implicit cast. – kobi Nov 16 '21 at 14:38
  • 1
    You're right that "implicit cast" is commonly used by beginners to describe an **implicit conversion**. Nevertheless, a cast is something you write in your source code. There is no such thing as an implicit cast. – Pete Becker Nov 16 '21 at 14:48
  • 1
    Thanks for the clarification. – kobi Nov 16 '21 at 15:05
  • Yep! But whiteWinning_ being changed effectively modifies the object that is the same as being pointed to by const Game& game, and that's okay because that value is seperate?? – Miles C. Nov 17 '21 at 07:07
  • 1
    The const occupies `game` object. `whiteWinning_ ` is an `Advantage` class member however the `this` in this context is non const – kobi Nov 17 '21 at 07:29