Having the struct with const function, what will be printed as a result of program execution?
#include <iostream>

struct A {
    int val_;

    A(int v) : val_(v) { } 

    void setPrintVal(int v) const
    {
         val_ += v;
         std::cout << val_ << endl;
    }
};

int main()
{
   A a(10);
   int value = 20;
   a.setPrintVal(++value);
}
Explanation
The function A::setPrintVal(int v) const is declared as constant, therefore, class members cannot be changed in it. You can work around this error by declaring a member of the class val_ as mutable.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести