problem with const member function and returning *this

This is a discussion on problem with const member function and returning *this within the C++ Programming forums, part of the General Programming Boards category; I'm reading a book and doing an exercise where I just basically add some members, one of which is a ...

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    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

    Code:
    class Screen
    {
    	public:
    		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; }
    		
    	private:
    		void do_display(std::ostream &out) {out << contents;}
    		
    		std::string contents;
    		index cursor;
    		index height, width;
    };
    The error is as follows
    [code]
    Screen.h:21: error: passing `const Screen' as `this' argument of `void Screen::do_display(std::ostream&)' discards qualifiers
    [code]

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

    "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
    Posts
    1,901
    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
    Location
    New Zealand
    Posts
    6,308

    :-)

    Yup. You're on to it.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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, 07:39 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  3. problem returning struct member from function
    By yukster in forum C Programming
    Replies: 6
    Last Post: 08-24-2003, 02:21 PM
  4. public function returning const char * of private member
    By VanJay011379 in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2002, 04:28 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

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