c++ 智能指针 shared_ptr

# include
# include      //atoi
# include     //strlen
# include    //uint64_t
#include
#include  //shared_ptr
using namespace std;

class A{    
public:
    template<class T> void sumA(T a ,T b,std::shared_ptr<int>* tensor);

    A(){
        printf("this is a construc function!\n");
    }
};
template<> void A::sumA<int>(int a,int b,shared_ptr<int>* tensor){
    printf("the sum of A is %d\n",a+b);
     *tensor = std::make_shared<int>(5);

}
int main(){
    A a;
    int b=10;
    shared_ptr<int>tensor;
    a.sumA<int>(1,2,&tensor);
    cout<<"share_ptr:"<<*tensor<return 0;
}

输出:

this is a construc function!
the sum of A is 3
share_ptr:00871244
请按任意键继续. . .

例子2:

#include        // std::cout
#include          // std::atomic, std::memory_order_relaxed
#include          // std::thread
#include
#include 
#include 
#include 
#include 
using namespace std;

int main ()
{
    int a=1;

    TorchOpContext<int>  op1=TorchOpContext<int>(1,&a);
    std::shared_ptr<int> b;
    b=make_shared<int>(30);
    cout<<*b<//op1.AllocatePersistent(10,&b);
    std::shared_ptr<int> c=b;
    *c=20;
    cout<<"对c进行改变,b的值:"<<*b<std::shared_ptr<int>*d=&b;

    d=&make_shared<int>(30);

    cout<<"d指针,指向另一个地址,b的值"<<*b<return 0;
}

输出结果:

30
对c进行改变,b的值:20
d指针,指向另一个地址,b的值20
请按任意键继续. . .

你可能感兴趣的:(c-c++)