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.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes