I´ve been trying to modify a data-member in const member function on a const object but I´ve been unsuccesful. I´ve been reading a book and found a code example on the net (http://cplus.about.com/library/gloss...ef-mutable.htm) on this subject, but still I can´t get it. Here is the code
Code:
#include <iostream>
#include <string>
using namespace std;
class Animal {
public:
void setAge(int age);
int getAge() const {return itsage;}
Animal(string aname, string afood, int age);
private:
string name;
string food;
mutable int itsage;
};
Animal::Animal(string aname, string afood, int age)
{
name = aname;
food = afood;
itsage = age;
}
void Animal::setAge(int age)
{
itsage = age;
}
int main()
{
Animal Tiger(string("Tigerman"), string("Meat"), 8);
//const Animal Tigermania(string("Tigermania"), string("moremeat"), 10);
Tiger.setAge(90);
cout << "Tigerman is " << Tiger.getAge() << " year(s) old" << endl;
return 0;
}
A get a compiler-error on the line
Code:
const Animal Tigermania(string("Tigermania"), string("moremeat"), 10);
Apperently I´m using it wrong because I´ve compiled it under two different compilers and get the same error.