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
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