Click to See Complete Forum and Search --> : consts


C#er
July 14th, 2008, 04:23 PM
Hi for all

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


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:

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

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

Graham
July 14th, 2008, 04:34 PM
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?

Lindley
July 14th, 2008, 04:34 PM
The answer is right here:
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.

sszd
July 14th, 2008, 04:40 PM
If I redefine the method 'GetString()' of the class Test to

inline string GetString const (){ return str; }


First of all, this is incorrect. The correct definition should be...

std::string GetString () const { return str; }

Secondly, t is a reference to a const object, therefore it can only call const methods.

exterminator
July 15th, 2008, 11:01 AM
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).

stephendoyle75
July 15th, 2008, 05:32 PM
First of all, this is incorrect. The correct definition should be...

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:

const std::string& GetString() const { return str; }

This avoids a potentially unnecessary string copy.