Re: member variable of STL string class
- From: "Alex Blekhman" <tkfx.REMOVE@xxxxxxxxx>
- Date: Fri, 22 Aug 2008 11:53:02 +0300
"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
.
- References:
- member variable of STL string class
- From: George
- Re: member variable of STL string class
- From: Ulrich Eckhardt
- Re: member variable of STL string class
- From: Bo Persson
- Re: member variable of STL string class
- From: George
- Re: member variable of STL string class
- From: Alex Blekhman
- Re: member variable of STL string class
- From: George
- member variable of STL string class
- Prev by Date: native MD5 API?
- Next by Date: Re: proxy stub code
- Previous by thread: Re: member variable of STL string class
- Next by thread: Re: member variable of STL string class
- Index(es):
Relevant Pages
|