CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: consts

  1. #1
    Join Date
    Oct 2005
    Posts
    173

    consts

    Hi for all

    I've defined these 2 classes in a cpp file(just for a single test):

    Code:
    using namespace std;
    
    class Test
    {
    	public:
    		Test(){}
    		~Tese(){}
    		inline string GetString()  { return str; }
    	private:
    		string str;
    };
    
    class Test2
    {
    	public:
    		Test2();
    		~Test2();
    		inline string GetString( const Test& t )
    		{
    			string ret = t.GetString();
    			return ret;
    		}
    	private:
    		string str;
    };
    When I compile this code, the following error message occurs:
    Code:
    error: passing 'const Teste' as 'this' argument of 'std::string Teste::GetString()' discards qualifiers
    The function of the class Test2 has the same name but only receives a const object of type Test. Simple, right? But errors are occuring

    If I redefine the method 'GetString()' of the class Test to
    Code:
    inline string GetString const (){ return str; }
    the compiler stops to complain.
    My doubt is: why do I need to insert a 'const' at the end of the GetString() method of the Test class if this const indicates to me that I don't change anything of this class inside the method?

    Fellows thanks for the big help and sorry for my english

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: consts

    The compiler will allow you to call only a const function on a const object (or reference/pointer-to-const). If it allowed you to call a non-const function, it would render the whole concept of "const" pointless. Can you imagine what would happen if you were just allowed to modify anything, even though you carefully defined it so that it can't be changed?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: consts

    The answer is right here:
    Code:
    inline string GetString( const Test& t )
    		{
    			string ret = t.GetString();
    			return ret;
    		}
    You've just said "const Test& t", promising the compiler that t will not be modified by the function. But then you call GetString(). Unless you've promised the compiler that GetString() will not modify t either, the compiler assumes it will and freaks out because it thinks you're trying to break your previous promise.

  4. #4
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    Re: consts

    Quote Originally Posted by C#er
    If I redefine the method 'GetString()' of the class Test to
    Code:
    inline string GetString const (){ return str; }
    First of all, this is incorrect. The correct definition should be...
    Code:
    std::string GetString () const { return str; }
    Secondly, t is a reference to a const object, therefore it can only call const methods.

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: consts

    Just on a side note, when you provide the definition of the member function inside the class definition, the inline keyword is redundant. You mark it as an inline function by just doing that (and not using the keyword, even though it may be allowed). inline keyword is only required when you provide an out-of-class definition for the member functions (or even non-member functions).

  6. #6
    Join Date
    Apr 2007
    Location
    Ireland
    Posts
    81

    Re: consts

    Quote Originally Posted by sszd
    First of all, this is incorrect. The correct definition should be...
    Code:
    std::string GetString () const { return str; }
    Secondly, t is a reference to a const object, therefore it can only call const methods.
    Even better would be:
    Code:
    const std::string& GetString() const { return str; }
    This avoids a potentially unnecessary string copy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured