Mutable members in const member functions still const

This is a discussion on Mutable members in const member functions still const within the C++ Programming forums, part of the General Programming Boards category; Ive been trying to modify a data-member in const member function on a const object but Ive been unsuccesful. Ive ...

  1. #1
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    Mutable members in const member functions still const

    Ive been trying to modify a data-member in const member function on a const object but Ive been unsuccesful. Ive 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 cant 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 Im using it wrong because Ive compiled it under two different compilers and get the same error.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Its a little perverse doing this IMO

    Ok...to envoke this on a const object, you can make the setAge const (even though it is assigning to a mutable)...this will then allow the func to be envoked on a const object....

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Animal {
    public:
    	void setAge(int age)const;
     	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)const
    {
    	itsage = age;
    }
    
    int main()
    {
        Animal Tiger(string("Tigerman"), string("Meat"), 8);
        const Animal Tigermania(string("Tigermania"), string("moremeat"), 10);
    
    	cout << "Tiger is " << Tiger.getAge() << " year(s) old" << endl;
    	cout << "Tigermania is " << Tigermania.getAge() << " year(s) old" << endl;
       
    
        Tiger.setAge(90);
    	Tigermania.setAge(10);
        
    
        cout << "Tiger is " << Tiger.getAge() << " year(s) old" << endl;
    	cout << "Tigermania is " << Tigermania.getAge() << " year(s) old" << endl;
       
    return 0;
    }
    Hmm..seems to work...but I still see it as perverse

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Its a little perverse doing this IMO
    ....
    ....
    ....
    Hmm..seems to work...but I still see it as perverse
    LOL , a little??? I know what you are meaning but I wanted to se if it was possible. But what is more perverse is how they could "forget" the const on http://cplus.about.com/library/gloss...ef-mutable.htm.

    Just out of curiosity is the setAge function overloaded in this case??
    Code:
    void setAge(int age)const;
    void setAge(int age);
    Depending if an object is const or not the "right" function is invoked. But if I recall it right, a fuction is only overloaded when the parameter list differs.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    yes
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes & Collections
    By Max_Payne in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 12:06 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  4. Pointers to member functions using virtual methods
    By Will B-R in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2006, 06:59 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21