Smart pointers unique_ptr

class A
{
    int data_;
public:
    A(int data_1,int data_2){
        data_=data_1+data_2;
    }

    ~A() {
        std::cout<<__FUNCTION__<<":"<<data_<<std::endl;
    }

    void print() {
        std::cout<<__FUNCTION__<<":"<<data_<<std::endl;
    }
};

void func()
{
    {
        std::unique_ptr<A[]> uptr_A(new A[3]{{1,2},{3,4},{5,6}});
        for(int i=0;i<3;i++) {
            uptr_A[i].print();
        }
    }
    
    std::unique_ptr<int[]> uptr_int(new int[3]{1,2,3});
    for(int i=0;i<3;i++) {
        std::cout<<uptr_int[i]<<std::endl;
    }
}

int main(int argc,char* argv[])
{
    func();
}

Guess you like

Origin www.cnblogs.com/smallredness/p/10935642.html