Why GetMaxTries() shouldnt be a const

Hello!

Well first of all I should begin with saying that English isn’t my main language and I feel that this is a bit tricky to get my head around possibly because of the language barrier.

From what I understand is that const makes is impossible for me to change the variables inside the functions from anywhere other then where I declared its value. If that is so shouldn’t GetMaxTries() function be open if you would like to alter it if say you want difficulty level. Lets say hard is 4 guesses and easy 12 guesses.

Now please correct me if I’m wrong, didn’t understand what this is all about or w/e it could be :wink:

// a swede interested in programming :slight_smile:

EDIT: Apparently I got this idea wrong and I need some to work with it some more to understand whats it’s all about.

You seem to be describing a const variable. const at the end of a member function just means that function doesn’t modify any of the class’ data.

So if I understood it right, you cant change int to float or bool to string with const at the end of a function?

Thank you for your respond :slight_smile:

Nothing to do with what the types are. It’s all about the class’ data

class Person
{
public:
    void NonConstFunction(); //can change class data
    void ConstFunction() const; //can't change class data
private:
    int ID;
    std::string Name;
};

void Person::NonConstFunction()
{
    //both valid
    ID = 4;
    Name = "Bill";
}

void Person::ConstFunction() const
{
    //Not class data; fine to modify
    int FakeID = 3;
    FakeID = 4;
    //attempting to modify class data; invalid
    ID = 10;
    Name = "Jim";
}
1 Like

Ahhh, right now I get it!

Thanks a lot!
That made it super obvious.

I got a little bit lost at the whole class, header part and all but it’s making more sens by the time.
I get the concept but all the syntax between it all is a bit to wrap your head around.

Thanks for the respond and well done explaining :slight_smile:

Privacy & Terms