Hi for all
I've defined these 2 classes in a cpp file(just for a single test):
When I compile this code, the following error message occurs: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; };
The function of the class Test2 has the same name but only receives a const object of type Test. Simple, right? But errors are occuringCode:error: passing 'const Teste' as 'this' argument of 'std::string Teste::GetString()' discards qualifiers
If I redefine the method 'GetString()' of the class Test to
the compiler stops to complain.Code:inline string GetString const (){ return str; }
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




Reply With Quote