# toString() returning const string&

This topic is 3717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've a variable called val of Type Var. Var Class have a tostring() methods that returns const string& type. But When I compile it compiler is firing this Error on the following line.
key+"="+val.toString()+"; at=";


error: passing ‘const Var’ as ‘this’ argument of ‘const std::string& Var::toString()’ discards qualifiers

Code of toString() method
const string& Var::toString(){
return (const string&)__data;//__data is of type string (just string not a reference or a const)
}



##### Share on other sites
toString() should be a const method, since you are not changing the state of Var in it.

##### Share on other sites
It looks like "val" is const. Declaring the toString() method as const also should fix the error.

Also, you don't need to cast the string to a const reference, that happens implicitely, if __data is indeed a string member, as you claim.

const string& Var::toString() const {return __data;//__data is of type string (just string not a reference or a const)}

##### Share on other sites
Quote:
 Original post by Driv3MeFartoString() should be a const method, since you are not changing the state of Var in it.
Sorry but toString() method is already const
Quote:
 Original post by RattenhirnIt looks like "val" is const. Declaring the toString() method as const also should fix the error.Also, you don't need to cast the string to a const reference, that happens implicitely, if __data is indeed a string member, as you claim.*** Source Snippet Removed ***
ya Its
const Var& val

I've also tried just keeping return __data but that was firing the same error on the same position.
again __data is
private:  string __data;

##### Share on other sites
Oh! whats the difference while attaching a const at the end ??
I did put const at the end But still the same Error on the same position.
and I dont understand what does that error actually mean

##### Share on other sites
Quote:
 Original post by nlbsOh! whats the difference while attaching a const at the end ??I did put const at the end But still the same Error on the same position.

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.10

Quote:
 Original post by nlbsI've also tried just keeping return __data but that was firing the same error on the same position.again __data is

That's because the cast doesn't actually do anything. But it will cause or hide errors when you decide to change __data in the future, so it's better to get rid of it.

If declaring toString as const doesn't remove the error, you need to post more code.

##### Share on other sites
Making a function const does not mean that its return is const, it means that you actually have to mark the function as being a constant function.

In other words, doing this is not enough, since the compiler has no idea if the function modifies the "this" pointer.
class Foo{public:    void doSomething(); //Does this modify data? We have no idea.private:    int data;};

But if you mark it as const, then the compiler knows that nothing* in the class can be changed when the function is executing.

class Foo{public:    void doSomethingConst() const; //We don't modify data in this function!private:    int data;}

The practical upshot of all this is that you can only call const functions from a const instance.

Foo nonConst;const Foo constFoo;nonConst.doSomething(); //FinenonConst.doSomethingConst(); //FineconstFoo.doSomethingConst(); //FineconstFoo.doSomething(); //FAIL! You cannot call a non-const function from a const instance.

So, in your code, you have a
const Var& val;//And you attempt to call Var::toString(); which is not const. You have to define it as a const function, by appending a const to the end of the function declaration.

* except for mutable members, but thats kinda a non-issue here.

EDIT:
Well, if thats not the problem, you might have to paste more code so we can see whats actually going wrong.

##### Share on other sites
Quote:
 Original post by nlbsOk here is my var.hhttp://pastebin.com/m526f26acand here is my var.cpphttp://pastebin.com/m59574521

You added the const in the .cpp file, but not in the .h file.

You might also make all the other accessors like "toInt" and "toLong" const, to avoid similar problems in the future.

Basically, every method, that does not modify any fields of the class should be declared as const.

##### Share on other sites
Most notably, your Var::toString() is still a non-const function.

In order to make it const, you should change it so that it looks like
const string& toString() const;//                       ^^^^^ Emphasis here.

Be sure to change your code in the .cpp file so that the function matches the signature in your .h file.

In other news, it seems that you are attempting to write a class that is able to hold variables of multiple types. I'd strongly advise you to look into boost::any
or boost::variant instead of doing (potentially) dangerous string casts.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
13
5. 5

• 14
• 10
• 23
• 9
• 57
• ### Forum Statistics

• Total Topics
632639
• Total Posts
3007606
• ### Who's Online (See full list)

There are no registered users currently online

×