We know that in C + +, destructors are executing destructors at the end of a function's scope to release objects, but in one case, the scope of the destructor changes, see the following example, define a stock class, The stock class is stored in Stock.h, and the main calling function is in Stock.cpp.
Stock.h
1#include <iostream>2#include <string.h>3 classstock{4 Private:5 Charm_company[ -];6 intM_stock_num;7 floatM_stock_price;8 floatTotal_price;9 floatStock_sum () {total_price= m_stock_num*M_stock_price;}Ten One Public: A intGet_stock (Char*company,intStock_num,floatstock_price); - voidIncrease (intNumfloatPrice ); - voidSellintNumfloatPrice ); the voidUpdate_price (floatPrice ); - voidprint_stock_info (); -Friend Stock Plus1 (intstock_num,stock t); -Stockoperator+(Stock t); +Stock (Char*p,intNumfloatPrice ); - Stock (); +~Stock (); A }; at -Stock::stock (Char*company,intStock_num,floatStock_price) - { - intlen=strlen (company); -strncpy (m_company,company,len+1); -m_stock_num=Stock_num; inM_stock_price=Stock_price; - stock_sum (); to + } - the Stock::stock () * { $m_stock_num=0;Panax NotoginsengM_stock_price=0; - stock_sum (); thestd::cout<<"Hello Boy"<<Std::endl; + } A thestock::~Stock () + { -std::cout<<"Good bye"<<Std::endl; $ $ } - -Stock Plus1 (intStock_num,stock t) the { - Stock Total;Wuyitotal.m_stock_num=stock_num+T.m_stock_num; the returnTotal ; - Wu } - About $Inline Stock Stock::operator+(Stock t) - { - Stock Total; -total.m_stock_num=m_stock_num+T.m_stock_num; ATotal.total_price=total_price+T.total_price; + returnTotal ; the } - $InlineintStock::get_stock (Char* Company,intStock_num,floatStock_price) the { the intlen=strlen (company); the if(len> -) the return false; -strncpy (m_company,company,len+1); inm_stock_num=Stock_num; theM_stock_price=Stock_price; the stock_sum (); About return true; the } the the voidStock::increase (intNumfloatPrice ) + { -m_stock_num=m_stock_num+num; theM_stock_price=Price ;Bayi stock_sum (); the } the - voidStock::sell (intNumfloatPrice ) - { the if(num>m_stock_num) them_stock_num=0; the Else them_stock_num=m_stock_num-num; - theM_stock_price=Price ; the stock_sum (); the }94 voidStock::update_price (floatPrice ) the { theM_stock_price=Price ; the 98 } About voidStock::p rint_stock_info () - {101std::cout<<m_stock_num<<Std::endl;102std::cout<<total_price<<Std::endl;103}
Stock.cpp
1#include"stock.h"2#include <iostream>3#include <cstring>4 using namespacestd;5 intMain ()6 {7 Char*p;8p=New Char[ -];9strcpy (P,"Baidu");TenStockObject[4]; One Object[0].get_stock (p,100,2.3); A Object[1].get_stock (p,100,2.3); - object[2]=object[0]+object[1]; - object[2].print_stock_info (); the Object[3]=plus1 ( -,Object[2]); - Object[3].print_stock_info (); - return true; - +}
You can see that after we declare the friend function Plus1, we pass the object[2] object through the constructor to the friend, and then call the copy constructor in the friend function to accept the parameter, then call the default constructor to form the local total object, after we have finished adding the action, the object will return, The destructor is called at this point, but if we take the return value of the reference as the Stock &plus1 (int stock_num,stock t) , the call succeeds, but the object is destroyed, and why it can still be referenced and continue to be used. Let's look at the results of the two return methods:
Returns the object,
Returns the object reference,
At this point the compiler should be optimized, because the function at the end of the scope, you should call the destructor (can be seen, called 5 constructors, 6 destructors, one copy constructor), so the destructor is definitely executed, but the compiler has been optimized processing, so that the object can still be passed, The compiler will now give you a warning prompt. But in 1 functions, we can return a reference to an object that is not disposed at the end of a function call. For example, an externally-passed object, such as a member variable of a class that the function belongs to. It is dangerous to return a local variable (object reference), but it is possible to return an object because the copy constructor is called when the object is returned, and then the destructor is. If we pass a reference to the PLUS1 call, the result is
, the copy constructor is not called. When the value is passed, the copy constructor is called again.
The scope of constructors and destructors in C + +