Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #19084 > unrolled thread

casting from 'const string' to a 'non-constant string'

Started byRene Ivon Shamberger <arbolone@gmail.com>
First post2012-10-19 05:36 -0700
Last post2012-10-19 21:43 +0100
Articles 4 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  casting from 'const string' to a 'non-constant string' Rene Ivon Shamberger <arbolone@gmail.com> - 2012-10-19 05:36 -0700
    Re: casting from 'const string' to a 'non-constant string' Juha Nieminen <nospam@thanks.invalid> - 2012-10-19 12:55 +0000
    Re: casting from 'const string' to a 'non-constant string' Victor Bazarov <v.bazarov@comcast.invalid> - 2012-10-19 14:42 -0400
    Re: casting from 'const string' to a 'non-constant string' Andy Champ <no.way@nospam.invalid> - 2012-10-19 21:43 +0100

#19084 — casting from 'const string' to a 'non-constant string'

FromRene Ivon Shamberger <arbolone@gmail.com>
Date2012-10-19 05:36 -0700
Subjectcasting from 'const string' to a 'non-constant string'
Message-ID<007be9fb-b37a-4fd7-8778-f17994b52fb4@googlegroups.com>
const std::string& someClass::someMethod(){ return some_string = "Bla Bla Bla"; }
....
const std::string& myMethod(){
 someClass obj;
 return obj.someMethod();
}

Left as it is, this example will give me a warning stating that the return value from myMethod is a local value, but if I change the code to:

const std::string& 
someClass::someMethod(){ return some_string = "Bla Bla Bla"; }
const std::string& myMethod(){
 someClass obj;
 std::string tmp = obj.someMethod(); /// New code
 return temp;
}
The compiler complains saying that conversion from 'const string' to 'string' is not permited.
How can I remove this error?

[toc] | [next] | [standalone]


#19086

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-10-19 12:55 +0000
Message-ID<k5rijs$9kn$1@speranza.aioe.org>
In reply to#19084
Rene Ivon Shamberger <arbolone@gmail.com> wrote:
> const std::string& 
> someClass::someMethod(){ return some_string = "Bla Bla Bla"; }
> const std::string& myMethod(){
> someClass obj;
> std::string tmp = obj.someMethod(); /// New code
> return temp;
> }

You should always post code that actually *compiles* rather than typing
at as you go. (For instance, the name 'temp' is undeclared there.)

Anyways, I don't see what causes the error message (probably something
you *didn't* write above), but I do know that you are returning a reference
to a temporary, which is erroneous. (Undefined behavior, may cause the
program to crash or misbehave in some othe rway.)

[toc] | [prev] | [next] | [standalone]


#19096

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2012-10-19 14:42 -0400
Message-ID<k5s704$cs9$1@dont-email.me>
In reply to#19084
On 10/19/2012 8:36 AM, Rene Ivon Shamberger wrote:
> const std::string& someClass::someMethod(){ return some_string = "Bla Bla Bla"; }

What's 'some_string'?  A member?  A global variable?

> ....
> const std::string& myMethod(){
>   someClass obj;
>   return obj.someMethod();

Right after this expression is evaluated and its value is prepared to be 
returned, the 'obj' object is destroyed.  *If* 'someMethod' returns part 
of the object for which it's called, as a reference to const, that 
reference becomes *invalid* as soon as 'obj' is destroyed, i.e. outside 
of the 'myMethod' function.  IOW, you can't use the return value of the 
'myMethod' function at all - that's undefined behavior.

> }
>
> Left as it is, this example will give me a warning stating that the return value from myMethod is a local value, but if I change the code to:
>
> const std::string&
> someClass::someMethod(){ return some_string = "Bla Bla Bla"; }
> const std::string& myMethod(){
>   someClass obj;
>   std::string tmp = obj.someMethod(); /// New code
>   return temp;
> }
> The compiler complains saying that conversion from 'const string' to 'string' is not permited.

On which line?  What's 'temp'?  Is it a global variable?  Where is this 
'myMethod' function defined?  A namespace scope or inside another class?

> How can I remove this error?

Don't "remove this error".  Code correctly.  Do not return references to 
local objects.  Ever.

Also, read the FAQ 5.8.

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#19103

FromAndy Champ <no.way@nospam.invalid>
Date2012-10-19 21:43 +0100
Message-ID<3MadnfWWfN3pJhzNnZ2dnUVZ8iudnZ2d@eclipse.net.uk>
In reply to#19084
On 19/10/2012 13:36, Rene Ivon Shamberger wrote:
> const std::string& someClass::someMethod(){ return some_string = "Bla Bla Bla"; }
> ....
> const std::string& myMethod(){
>   someClass obj;
>   return obj.someMethod();
> }
>
> Left as it is, this example will give me a warning stating that the return value from myMethod is a local value, but if I change the code to:
>
> const std::string&
> someClass::someMethod(){ return some_string = "Bla Bla Bla"; }
> const std::string& myMethod(){
>   someClass obj;
>   std::string tmp = obj.someMethod(); /// New code
>   return temp;
> }
> The compiler complains saying that conversion from 'const string' to 'string' is not permited.
> How can I remove this error?
>

After Victor and Juha's comments... I'm guessing that in your actual 
code it reads "const std::string& tmp = obj.someMethod(); /// New code"

... in which case you're copying the reference into the local variable 
and returning it, but the thing it refers to isn't a temporary. IYSWIM.

Andy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web