Thread: toString() const string&

  1. #1
    Registered User
    Join Date
    Jun 2007

    toString() const string&

    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)

  2. #2
    Registered User
    Join Date
    May 2008
    don't return a const type, that doesn't make sense here. Why do you return a reference and not just an object?

    Const-correctness is something that applies to arguments of functions, not of their return types. Remember: try to avoid declaring variables as 'const' (like in a class).

    string Var::toString()
    	return static_cast<string>(__data);
    A nice example why you should use C++ style casts : what does '(const string&)__data' do?
    Last edited by MarkZWEERS; 07-22-2008 at 01:33 AM.

  3. #3
    The larch
    Join Date
    May 2006
    This function should be declared as const (because it returns data and doesn't modify it), otherwise you can't call it on constant instances. E.g:
    void foo(const Var& v)
         v.toString(); //discards qualifiers error, unless toString is a const method
    As to casts:
    if you want to return a reference, you don't need a cast. This is how you normally make a reference to something (i doesn't have to be a reference for a reference to refer to it):
         int i = 42;
         int& ref = i; //note: no cast!
    If __data is a std::string then casting it to std::string is meaningless.

    I seem to remember that you had other rather meaningless casts in your code, such as:
    Var x = (Var)42;
    If it didn't already work without that cast (because Var has a non-explicit constructor that takes an integer argument), then the cast wouldn't do the right thing either. Normal ways to declare and initialize an instance:
    Var x(42);
    Var x = 42; //if the constructor is not explicit
    Var x = Var(42); //copy from a temporary
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jun 2007
    exactly it fixed after I changed it to
    const string& toString() const{

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM