打印

关于智能指针和拷贝构造函数的一个例子

[复制链接]
219|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
keer_zu|  楼主 | 2022-10-9 16:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
代码:
#include <iostream>
#include <memory>

using namespace std;

class B;    //声明
class A
{
public:
    shared_ptr<B> pb_;
    ~A()
    {
        cout << "A delete\n";
    }
};

class B
{
public:
    shared_ptr<A> pa_;
    ~B()
    {
        cout << "B delete\n";
    }
};

void test_w()
{
    shared_ptr<B> pb(new B());
    shared_ptr<A> pa(new A());
    cout << pb.use_count() << endl;    //1
    cout << pa.use_count() << endl;    //1
    pb->pa_ = pa;
    pa->pb_ = pb;
    cout << pb.use_count() << endl;    //2
    cout << pa.use_count() << endl;    //2
}



void test_shared()
{
    string *s1 = new string("s1");

    shared_ptr<string> ps1(s1);
    shared_ptr<string> ps2;
    cout << ps1.unique() << endl;
    cout << ps2.unique() << endl;
    ps2 = ps1;

    cout << ps1.use_count()<<endl;    //2
    cout<<ps2.use_count()<<endl;    //2
    cout << ps1.unique()<<endl;    //0

    string *s3 = new string("s3");
    shared_ptr<string> ps3(s3);

    cout << (ps1.get()) << endl;    //033AEB48
    cout << ps3.get() << endl;    //033B2C50
    cout << ps1.use_count() << endl;
    cout << ps3.use_count() << endl;
    cout << __LINE__ << "_ps1:" << *ps1 << endl;
    cout << __LINE__ << "_ps3:" << *ps3 << endl;
    cout << __LINE__ << "_ps2:" << *ps2 << endl;
    swap(ps1, ps3);    //交换所拥有的对象
    cout << (ps1.get())<<endl;    //033B2C50
    cout << ps3.get() << endl;    //033AEB48
    cout << __LINE__ << "_ps1:" << *ps1 << endl;
    cout << __LINE__ << "_ps3:" << *ps3 << endl;
    cout << ps1.use_count()<<endl;    //1
    cout << ps2.use_count() << endl;    //2
    ps2 = ps1;
    cout << __LINE__ << "_ps2:" << *ps2 << endl;
    cout << __LINE__ << "_ps1:" << *ps1 << endl;
    cout << ps1.use_count()<<endl;    //2
    cout << ps2.use_count() << endl;    //2
    ps1.reset();    //放弃ps1的拥有权,引用计数的减少
    cout << ps1.use_count()<<endl;    //0
    cout << ps2.use_count()<<endl;    //1
}


int test2()
{
    //初始化方式1
    std::unique_ptr<int> up1(new int(123));
    //初始化方式2
    std::unique_ptr<int> up2;
    up2.reset(new int(456));
    //初始化方式3 (-std=c++14)
    std::unique_ptr<int> up3 = std::make_unique<int>(789);
    cout << *up1 << " " << *up2 << " " << *up3 << endl;
}

class Complex
{
public:
        ~Complex(){cout << "_Destory_" << __LINE__ << endl;}

        Complex(int,int){cout << "_Creat_" << __LINE__ << endl;}
        Complex(const Complex&){cout << "_Creat_cp_" << __LINE__ << endl;}
};
void Fun(Complex c1)
{
}
Complex Fun2()
{
  Complex c(10,20);
  return c;
}

void test3()
{
        cout << "c1:" << endl;
  Complex c1(1,2);
  cout << "Fun(c1):" << endl;
  Fun(c1);
  cout << "c2:" << endl;
  Complex c2 = Fun2();
  cout << "c3:" << endl;
  Complex &c3 = c2;
}

int main()
{
        test3();
        test2();
        test_w();
        //test_shared();
        /*
        unique_ptr<string> ps1, ps2;
        ps1 = unique_ptr<string>(new string ("Hello"));
        ps2 = move(ps1);
        ps1 = unique_ptr<string>(new string ("nihao"));
        cout << *ps2 << " " << *ps1 << endl;
        */
    return 0;
}

使用特权

评论回复
沙发
keer_zu|  楼主 | 2022-10-9 16:32 | 只看该作者
运行结果:


zukeqiang@35fb9925ca21:~/test$ ./oz
c1:
_Creat_100
Fun(c1):
_Creat_cp_101
_Destory_98
c2:
_Creat_100
c3:
_Destory_98
_Destory_98
123 456 789
1
1
2
2

使用特权

评论回复
板凳
keer_zu|  楼主 | 2022-10-9 16:44 | 只看该作者
例子做了修改:

#include <iostream>
#include <memory>

using namespace std;

class B;    //声明
class A
{
public:
    shared_ptr<B> pb_;
    ~A()
    {
        cout << "A delete\n";
    }
};

class B
{
public:
    shared_ptr<A> pa_;
    ~B()
    {
        cout << "B delete\n";
    }
};

void test_w()
{
    shared_ptr<B> pb(new B());
    shared_ptr<A> pa(new A());
    cout << pb.use_count() << endl;    //1
    cout << pa.use_count() << endl;    //1
    pb->pa_ = pa;
    pa->pb_ = pb;
    cout << pb.use_count() << endl;    //2
    cout << pa.use_count() << endl;    //2
}



void test_shared()
{
    string *s1 = new string("s1");

    shared_ptr<string> ps1(s1);
    shared_ptr<string> ps2;
    cout << ps1.unique() << endl;
    cout << ps2.unique() << endl;
    ps2 = ps1;

    cout << ps1.use_count()<<endl;    //2
    cout<<ps2.use_count()<<endl;    //2
    cout << ps1.unique()<<endl;    //0

    string *s3 = new string("s3");
    shared_ptr<string> ps3(s3);

    cout << (ps1.get()) << endl;    //033AEB48
    cout << ps3.get() << endl;    //033B2C50
    cout << ps1.use_count() << endl;
    cout << ps3.use_count() << endl;
    cout << __LINE__ << "_ps1:" << *ps1 << endl;
    cout << __LINE__ << "_ps3:" << *ps3 << endl;
    cout << __LINE__ << "_ps2:" << *ps2 << endl;
    swap(ps1, ps3);    //交换所拥有的对象
    cout << (ps1.get())<<endl;    //033B2C50
    cout << ps3.get() << endl;    //033AEB48
    cout << __LINE__ << "_ps1:" << *ps1 << endl;
    cout << __LINE__ << "_ps3:" << *ps3 << endl;
    cout << ps1.use_count()<<endl;    //1
    cout << ps2.use_count() << endl;    //2
    ps2 = ps1;
    cout << __LINE__ << "_ps2:" << *ps2 << endl;
    cout << __LINE__ << "_ps1:" << *ps1 << endl;
    cout << ps1.use_count()<<endl;    //2
    cout << ps2.use_count() << endl;    //2
    ps1.reset();    //放弃ps1的拥有权,引用计数的减少
    cout << ps1.use_count()<<endl;    //0
    cout << ps2.use_count()<<endl;    //1
}


int test2()
{
    //初始化方式1
    std::unique_ptr<int> up1(new int(123));
    //初始化方式2
    std::unique_ptr<int> up2;
    up2.reset(new int(456));
    //初始化方式3 (-std=c++14)
    std::unique_ptr<int> up3 = std::make_unique<int>(789);
    cout << *up1 << " " << *up2 << " " << *up3 << endl;
}

class Complex
{
private:
    int Real;
    int Image;

public:
        ~Complex(){cout << "_Destory_" << __LINE__ << endl;}

        Complex(int r,int i):Real(r),Image(i){cout << "_Creat_" << __LINE__ << endl;}
        Complex(const Complex& c):Real(c.Real),Image(c.Image){cout << "_Creat_cp_" << __LINE__ << endl;}
};
void Fun(Complex c1)
{
}
Complex Fun2()
{
  Complex c(10,20);
  return c;
}

void test3()
{
        cout << "c1:" << endl;
  Complex c1(1,2);
  cout << "Fun(c1):" << endl;
  Fun(c1);
  cout << "c2:" << endl;
  Complex c2 = Fun2();
  cout << "c3:" << endl;
  Complex &c3 = c2;
  cout << "c4:" << endl;
  Complex c4 = c3;
  cout << "c5:" << endl;
  Complex c5(c4);
  cout << "c5 value:"  << endl;
}

int main()
{
        test3();
        test2();
        test_w();
        //test_shared();
        /*
        unique_ptr<string> ps1, ps2;
        ps1 = unique_ptr<string>(new string ("Hello"));
        ps2 = move(ps1);
        ps1 = unique_ptr<string>(new string ("nihao"));
        cout << *ps2 << " " << *ps1 << endl;
        */
    return 0;
}

使用特权

评论回复
地板
keer_zu|  楼主 | 2022-10-9 16:45 | 只看该作者
结果:


zukeqiang@35fb9925ca21:~/test$ ./oz
c1:
_Creat_104
Fun(c1):
_Creat_cp_105
_Destory_102
c2:
_Creat_104
c3:
c4:
_Creat_cp_105
c5:
_Creat_cp_105
c5 value:
_Destory_102
_Destory_102
_Destory_102
_Destory_102
123 456 789
1
1
2
2

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1314

主题

12271

帖子

53

粉丝