Temporary objects for C + + production and use

Source: Internet
Author: User



The so-called temporary object is a nameless object.

The hypothesis of its appearance is not under the expectation of the program ape (for example, whatever the pass by value operation will cause a copy operation, thus forming a temporary object), often as a burden of efficiency.

But sometimes it is possible to create some temporary objects, but also to make the program clean and refreshing skills.

The way to deliberately create a temporary object is. Add a pair of parentheses directly after the type name and specify the initial value, such as Int (8), which is equivalent to calling the corresponding constructorand does not specify the object name. STL Most often applies this technique to the collocation of imitation functions and algorithms.

Like what:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace Std;

Template <typename t>

Class Print

{

Public:

Voidoperator () (const t& Elem)

             {

cout<< elem << ";

             }

};

int main (int argc, char* argv[])

{

Intia[6] = {0,1,2,3,4,5};

Vector<int>iv (ia,ia+6);

For_each (Iv.begin (), Iv.end (),print<int> ());

}

PRINT<INT> This object is passed into the for_each () function. When at the end. This temporary object it ended its life.

copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Temporary objects produced and used by C + +

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.