toString() returning const string&

Started by
11 comments, last by Rattenhirn 15 years, 9 months ago
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)
}

Advertisement
toString() should be a const method, since you are not changing the state of Var in it.
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)}
Quote:Original post by Driv3MeFar
toString() 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 Rattenhirn
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.

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


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
Quote:Original post by nlbs
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.


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

Quote:Original post by nlbs
I'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.
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.

[size=1]Visit my website, rawrrawr.com

Ok here is my var.h (Edit: LOL whats the BBCode for the urls here ??)
http://pastebin.com/m526f26ac
and here is my var.cpp
http://pastebin.com/m59574521
Quote:Original post by nlbs
Ok here is my var.h
http://pastebin.com/m526f26ac
and here is my var.cpp
http://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.
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.

[size=1]Visit my website, rawrrawr.com

This topic is closed to new replies.

Advertisement