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 + +