Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > casting from 'const string' to a 'non-constant string'

Reply
Thread Tools

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

 
 
Rene Ivon Shamberger
Guest
Posts: n/a
 
      10-19-2012
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?
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      10-19-2012
Rene Ivon Shamberger <(E-Mail Removed)> 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.)
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-19-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
type casting vs. type converting Toby VHDL 3 09-07-2005 01:42 PM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM
'STD_LOGIC_VECTOR ' to 'unsigned' type casting Ben Nguyen VHDL 6 09-20-2003 05:09 PM



Advertisments