A different tack?

So, I had a bit of a philosophical issue assigning MAXTRIES as a const inside the FBBullCow object. My thinking here is that each time the object is instantiated – or, reset, even – the caller may wish to control the number of tries. Maybe that’s thinking too much (or, more likely, too little) about it, but I ended up:

in main.cpp

const int MAX_TRIES = 8;
.
.
.
BCGame.Reset(MAX_TRIES);

in FBullCowGame.h

private:
int MyMaxTries;

in FBullCowGame.cpp

FBullCowGame::FBullCowGame()
{
Reset(1); \explcitly set to 1 so that if it only loops once, I know I screwed the pooch…
return;
}

.
.
.
void FBullCowGame::Reset(int MyMaxTriesIN)
{
MyMaxTries = MyMaxTriesIN;
return;
}

So…the challenge is two-fold. One, first I tried to pass that into the constructor, but found out that was a no-no. Second, I see that by doing this, I cannot make int MyMaxTries const in the .h file – the compiler obviously doesn’t like me changing the value.

Is there another way to protect it from being munged? Is this a bad idea/approach?

We are using constexpr so the value of MAX_TRIES is set at compile time, not every time we “call it” so I think there’s no problem.

If you use const in the .h file, you need to declare the value there so it would be:
const int MyMaxTries = 8;

you can’t put const after a variable, it only works with methods
int MyMaxTries const would be an error.

Hope it’s not to late, and if you found out otherwise, please tell me.

2 Likes

Privacy & Terms