Thread: problem with const member function and returning *this

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001

    problem with const member function and returning *this

    I'm reading a book and doing an exercise where I just basically add some members, one of which is a const member function. It returns *this and I have it just like it has in the book, but it's giving me an error

    class Screen
    		typedef std::string::size_type index;
    		Screen(index h, index w, std::string c) : 
    					height(h), width(w), contents(c){}
    		Screen &move(index, index);
    		Screen &set(char);
    		Screen &set(index, index, char);
    		const Screen &display(std::ostream &os) const //error
                      { do_display(os); return *this; }                     //
    		Screen &display(std::ostream out)
    				{ do_display(out); return *this; }
    		void do_display(std::ostream &out) {out << contents;}
    		std::string contents;
    		index cursor;
    		index height, width;
    The error is as follows
    Screen.h:21: error: passing `const Screen' as `this' argument of `void Screen::do_display(std::ostream&)' discards qualifiers

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Make do_display const too. You can only call const functions from other const functions.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    so basically if a const member function also calls a non_const member function, I get the same error. Which is basically the point of const members to begin with, gotcha.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand


    Yup. You're on to it.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Yup. And a very powerful language functionality.

    I have caught many errors at compile time by being very generous in my usage of the const keyword. Basically, const is your friend.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. problem returning struct member from function
    By yukster in forum C Programming
    Replies: 6
    Last Post: 08-24-2003, 01:21 PM
  4. public function returning const char * of private member
    By VanJay011379 in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2002, 03:28 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM