Temporary object research

zhaozj2021-02-16  43

Temporary objects, very much in the C surface and internal implementation. However, due to its specific nature, many behaviors are not known. In the compile time program initiated by Andrei, it is very valuable, and we pass Experiments to verify some behaviors in order to better use it: #include #include

Using namespace std;

Class X; Void Fook __ (x _in);

Class x {public: x (int _param): m_idata (_param) {} ~ x () {cout << "i am des ..now .." << "<< m_idata << endl;} void print ) {cout << "signal ..." << endl;} public: int m_idata;

Void Fook (x _in) {_ in.print (); cout << "<< _ in.m_idata << endl;}

Void Fook _ () {x (100); Fook __ (x (1200));}

Void fook __ (x _in) {_ in.print (); x (1000) .print ();

Int main () {

// Int_IData; cout << "<< & _ idata << Endl; cout <<" << & x (10) << Endl;

//

{X (10); // [1]};

//

{x a (199);}; //

Cout << "----------------------" << Endl;

//

Fook_ (); // This place is very important

//

Cout << "----------------------" << Endl;

// X (10); // [2]

// Cout << "--------------------- - "<< Endl;

Fook (x (100)); // [3]

// /// From above: // "Instant" temporary object (contrast to Copy, etc. The life of nature is (;) so ... // 2. In all [things and things involved in the temporary object [things, no matter how many layers, // or some steps, there is no relationship and effect after a certain step ......] // End, the original temporary object (the function of the above function is Copy) will only destroy. // and "immediate" temporary object life is the shortest ....... .......// // I have a view of this phenomenon: // 1. The compiler identifies the temporary object and assigns space as it is the same as the normal variable, // However, in the compile period, the compiler Understand all of its behavior and data is used // At a specific time, call destructure ... but the designer here is not a pile object, it will not continue call :: operator delete (,) The so-called // release space, that in fact, "useless" is valid for the heap object ... This // Even if the destructor is called, what you see, the designer does not mean it, Memory is related to the temporary object on the stack (if your object is not complicated enough) .... // For temporary objects on the pile, it does not "handle", you will always release / / can't It couldn't help but also manage .... //, then you build one, Memory Leak is a little ........ // Beware of this behavior ........ ... / / Unless Delete X ("I am Programming ...."); // --------------------------- Look at this fragment ------------------------- / * # include #include

Using namespace std;

Class x {public: char * _chr_ptr; x (char * _IN): _CHR_PTR (_) {} /// char * _chr_ptr; // x (char * _in): _ chr_ptr (new char [strullin (_in) 1 ]) {// strcpy (_chr_ptr, _in); //} // Try this again ........... [2] // ~ x () {delete _chr_ptr; // Note this On the place, when the data on the connecting stack, there is no problem in connecting the data on the pile. Cout << this << Endl; // Note}}; INT main () {x ("I am programming ..."); // Note this temporary object getchar (); // If you use new x ("I am programming ..." Return 1; // Problem ... again combined [2], try again ... // Try you test: New X ("I am programming ....") and // delete X ("i am programmin ... ")} * / // // You look back ,,,,, imagine the object on the stack ... /// //// Summary: All clouds are fog, just how the compiler controls how the temporary object is interlaced // life management, still seeking ... ... /// /

GetChar (); Return 1;

-------------------------------------------------- -------------------- Don't think about the value of this, please see a delicate program: Temporary object solves multithreaded problems ---------- ------------------------------ Programme1: Class Widget {... void Lock (); // Enter the critical area Void Unlock (); // Exit the critical establishment of the critical area //, must be used to use the system function, simply .....}; Programme2: Template Class LockProxy {public: LockingProxy (t * pobj): Pointee_ (POBJ) {POINTEE _-> Lock ();} // In the temporary object structure, it is locked // weight object (critical area). ~ LockingProxy () {Pointee_-> unlock ();} // // In temporary object When destroy, exit the critical area. // T * Operator -> () const {return pointee_;} // // Heights here. To perform // request // request to the temporary object // PRIVATE: LOCKINGPROXY & OPERATOR = (const LockingProxy &); T * Pointee_;}; Programme3: Template class smartptr {... LockingProxy Operator -> () const {returnLockingProxy (Pointee_);} ///// The core is here: generating temporary objects // LockingProxy (Pointee_) private: st * pointee_;};

转载请注明原文地址:https://www.9cbs.com/read-23586.html

New Post(0)