下面是代码示例:
#include "stdafx.h"
#include <iostream>
#include <memory>
using std::cout;
using std::cin;
using std::endl;
using std::unique_ptr;
unique_ptr<double> treble(double);
int main(int argc,_TCHAR* argv[])
{
double num(5.8);
unique_ptr<double> ptr(nullptr);
ptr = treble(num);
cout<<endl<< "Three times num = " <<3.0*num;
cout<<endl<< "Result = " <<*ptr;
cout<<endl;
return 0;
}
unique_ptr<double> treble(double data){
unique_ptr<double> result(new double(0.0));
*result = 3.0*data;
return result;
}
示例解析: