|
-
July 14th, 2008, 04:23 PM
#1
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
-
July 14th, 2008, 04:34 PM
#2
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
-
July 14th, 2008, 04:34 PM
#3
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.
-
July 14th, 2008, 04:40 PM
#4
Re: consts
 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.
-
July 15th, 2008, 11:01 AM
#5
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).
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 15th, 2008, 05:32 PM
#6
Re: consts
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|