C + + Temporary objects

Source: Internet
Author: User

C + + really so-called temporary objects are not visible-it will not appear in your source code. As long as you produce a Non-heap object without naming it, you will be born with a temporary target. These anonymous objects typically occur in two situations:

One is when the implicit type conversion (implicit type conversions) is implemented in order for the function call to succeed;

The second is when the function returns the object;

In fact, there is a situation when you create a Non-heap object, which is a nameless object.

Here's a common and easy-to-ignore error:

#include <iostream>using namespace std;int main(int argc, char *argv[]) {    string s1("a"),s2("b");    const char * s = (s1+s2).c_str();    printf("%s\n",s);}

You may get the correct results when this program is running, but in fact the practice here is unsafe, the temporary objects generated by S1+S2 are refactored at the end of the expression, so if the trace stack finds that the memory space of s in the print statement has pointed to an unknown address.

Here are some examples of 3 instances where temporary objects are generated:

1. Implicit type conversions

void uppercasify(string& str);// changes all chars in str to upper casechar subtleBookPlug[] = "Effective C++";uppercasify(subtleBookPlug); //error!!!

Because for the function call to succeed, Subtlebookplug must be converted to a string type, and the compiler thinks you want to change the subtlebookplug, and the type conversion will result in a temporary object of type string, and in void Uppercasify ( string& str), the change will be the temporary object, not the Subtlebookplug, which is clearly not what the programmer expects, so C + + wisely prohibits this behavior

2. Function return Object

#include <iostream>using namespace std;int func() {    int itgr;    return itgr;}int main(int argc, char *argv[]) {    int in;    in = func();}

The return value in the function is copied to a temporary object in the called function stack as a copy of the value. Here the expression func () creates a temporary object that stores the object returned by the Func () function, the temporary object is constructed by a copy of the Itgr object returned by Func (), the temporary object is assigned a value to in, the assignment expression ends, and the temporary object is destructor. So there's a problem here.

int& intref = func();

If a temporary object is used to initialize the Intref reference, once the expression is executed, the life cycle of the temporary object is ended, and the referenced entity is no longer present.

Objects for 3.no Heap

int& intref = int(5);int itgr = int(5);Calculate(int(5));

The temporary objects in the above expressions are used to perform initialization references, copy constructs and arguments, and so on.

On the temporary object life cycle of the issue, the following blog update it, more and more feel C + + really troublesome ...

C + + temporary objects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.