Re: member variable of STL string class

Tech-Archive recommends: Speed Up your PC by fixing your registry



"George" wrote:
I have tried the code, no warning and output is
"msdn.microsoft.com", what is wrong?

string foo()
{
string b;
...
return b;
}


In the code above you do not return a reference to the local
variable `b'. Instead, you return a copy of `b'. It is OK to retun
copies of local objects from a function.

However, you have other problem:

int main()
{
string& s1 = foo();

cout << s1 << endl;

return 0;
}

You bind a non-constant reference to a temporary copy of an
object. This is unsafe and conflicts with C++ Standard. Compiler
rightfully warns you with C4239 warning about this. If you don't
see this warning, then go to project settings and select warning
level 4 in C/C++ category.

Alex


.



Relevant Pages

  • Re: Function Warning - Null Reference
    ... Warning 1 Function 'Dec2hms' doesn't return a value on all code paths. ... null reference exception could occur at run time when the result is used. ... default value for a String is String.Empty, the default value for an Integer ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Complex Specified Information - Pitman Formula
    ... Therefore a significant match between a reference and a test ... string is good evidence of non-random production. ... and there are no finite algorithms to compute their digits. ... probabilities of the different symbols the information source can produce. ...
    (talk.origins)
  • Re: String Reference Type
    ... All unary and binary operators have predefined implementations that are ... Therefore its always allocated in the heap and a variable of string ... As with all classes in this case y and x both reference the same String ... language depandant matter as below. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Abstract class variables question
    ... But as I think you've seen elsewhere in this thread, a value type can exist inside a class and in that case the value type is stored in the heap with the rest of the class instance. ... But as far as the "faster" goes, yes...to some extent value types have less overhead than reference types, and so can perform better in certain cases. ... Well, that would be true for a string object too, if there was any way to actually change a string. ... Seriously though, it is practically always the case that when you are writing an assignment to a reference, you're replacing the reference held by the variable. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Measurement of pitch
    ... as the method used by the Pythagoreans. ... of these reference units in the quantity to be measured. ... vibrating string seems as good as anything. ... The string or pendulum in question could no doubt be specified exactly, ...
    (sci.physics)