toString() const string&

This is a discussion on toString() const string& within the C++ Programming forums, part of the General Programming Boards category; I've a variable called val of Type Var. Var Class have a tostring() methods that returns const string& type. But ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    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.
    Code:
    key+"="+val.toString()+"; at=";
    Code:
    error: passing ‘const Var’ as ‘this’ argument of ‘const std::string& Var::toString()’ discards qualifiers
    Code of toString() method
    Code:
    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
    Location
    Paris
    Posts
    248
    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).

    Code:
    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
    Posts
    3,573
    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:
    Code:
    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):
    Code:
         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:
    Code:
    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:
    Code:
    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
    Posts
    219
    @annon
    exactly it fixed after I changed it to
    Code:
    const string& toString() const{
      //
    }
    Thanks

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, 04:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21