"Z Z Z ...", SolmyR is snoring again, although the snoring is banned by the group, but no one can stop Solmyr from dreaming of being prawn in sleep, teaching prawns in the realistic realistic prawn. In the dream, he is full of tricks, and the mouth has flowed a writing desk. I didn't expect to suddenly hear "stupid", smashed, seeing the Email sent by the test department, complaining that the program he wrote is not tested.
He put the program code into Ultraedit, looked at it, it turned out to be the string handler:
Void F (String & S1, String & S2)
{
Const char * cs = (S1 S2) .c_str ();
Cout << CS;
}
In his opinion, this program has no problem, he tried to test it, no problem, CS correctly showed the result, isn't it. "The dead test department, always inexplicably sent these meaningless emails ..." SolmyR mouth muttered, suddenly heard the sound from behind "Note the survival of the temporary object, child."
SolmyR was scared, it was Zero, he always appeared in your way, and helped you (although it would take a few ridiculous and ironic). What is this? "Child, do you know the survival of a temporary object?" "Hey, I think, probably, it should be after exiting its role (Scope), it is destructed." SolmyR face pale, expensive Reply.
"No, no, they will be destructed in the end of their expression (" TCPL ": a Temporary Object is destroyed at the end of the full full expression in which it was created. A Full Expression IS An Expression That Is IS NOT A SUBEXPRESSION OF SOME Other Expression, maybe you run your program again. "
SolmyR has run again, surprisingly this result is not the same as the last time, too exaggerated. At this time, Zero's voice sounded next to the ear "now, talk about why it is like this. SolmyR thinks, suddenly a big understanding:" Due to the temporary object generated by S1 S2, it is after the end of the expression. Destructured, the memory pointed to by the CS does not necessarily exist, maybe still S1 S2, or may not be, so it is not guaranteed to display correct. "
"Very good, can not always be so simple, C specifies, temporary objects can be used as the initializer of the constant reference and namject, just like the following:
Void F (Const String &, Const String &);
Void H (String & S1, String & S2)
{
Const string & s = s1 s2;
String ss = S1 S2;
f (s, ss);
}
The above code will run very well, and temporary objects are destroyed after the constant reference and naming objects exit their scope. Temporary objects often appear in the following occasions: type conversion and function returns. Function return value can be optimized by the compiler, so you don't have to worry about the overhead it brings. The type conversion is broken, and its purpose is generally in order to make the function calls can be successful, as follows: Void Uppercasify (String & STR);
// Changes All Chars in str to Upper case
CHAR SUBTLEBOOKPLUG [] = "EFFECTIVE C ";
Uppercasify (SUBTLEBOOKPLUG); // Error!
Why, can you tell me? "
"Because the function call is successful, the SubtleBookPlug must be converted to the String type, and the compiler thinks you want to change the subtlebookPlug, and the type conversion will generate a Timed object of String, and in Void Uppercasify (String & Str), Changed will be this temporary object, not SubtlebookPlug, which is obviously not expected by programmers, so C is noticeable to ban this behavior. "
"Very good, today you have a very good, my child, but remember, don't tell the bad things behind the test department, otherwise, ..."
Note:
All examples of this article are referred to.
The C Programming Language 3rd More Effective In C
If the reader feels that there is no understanding or is not addicted, you can refer to TCPL PG254-255, MEC ITEM 19, ITEM 20