美文网首页
chapter-13

chapter-13

作者: 峡迩 | 来源:发表于2017-08-06 19:02 被阅读0次
// chapter-13.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<string>
#include<iostream>
#include<vector>
#include<memory>

using namespace std;

class Example_Copy
{
public:
    Example_Copy() = default;
    Example_Copy(string s,vector<string> &p):id(s),phone(make_shared<vector<string>>(p)){}
    Example_Copy(string s):Example_Copy(s,vector<string>()){}

    Example_Copy(const Example_Copy &rhs):id(rhs.id), phone(rhs.phone) { };     //拷贝构造函数必须是引用类型!几乎此参数总是一个const的引用!发生在将新对象初始化为同类型另一个对象的副本!
    Example_Copy& operator=(const Example_Copy&rhs) { id = rhs.id; phone = rhs.phone; return *this; }   //拷贝赋值运算符!
    ~Example_Copy() {};                                                         //析构函数,销毁成员需要执行成员自己的析构函数,内置类型不需要析构函数!new构造的动态内存需要delete删除!
    //变量离开作用域时、对象被销毁其成员也被销毁、容器被销毁其元素也被销毁、动态分配的内存需要delete释放、临时对象结束时被销毁!引用和指针离开作用域时,析构函数不会执行!
    //需要析构函数,也需要拷贝和赋值!(动态分配内存)。需要拷贝操作也需要赋值操作,反之亦然!
    //Example_Copy(const Example_Copy &rhs)=delete!删除函数!,阻值拷贝或赋值操作!
private:
    string id;
    shared_ptr<vector<string>> phone;
};

//行为像值的类!
class Example_by_value
{
public:
    Example_by_value(const string &s=string()):ps(new string(s)),i(0){}
    Example_by_value(const Example_by_value &p):ps(new string(*p.ps)),i(p.i){}//拷贝构造函数
    Example_by_value& operator=(const Example_by_value &p);

private:
    string *ps;
    int i;
};
Example_by_value& Example_by_value::operator=(const Example_by_value &p)    //像值的拷贝赋值构造函数!
{
    auto newp = new string(*p.ps);
    delete ps;//释放对象拷贝赋值前的内存空间!
    ps = newp;
    i = p.i;
    return *this;
};

//定义像指针的类!推荐直接使用shared_ptr,如果想自己管理资源,则模仿shared_ptr引用计数器
class Example_by_point
{
public:
    Example_by_point(const string &s=string()):ps(new string(s)),i(0),use(new size_t(1)){}
    Example_by_point(const Example_by_point &p) :ps(p.ps), i(p.i), use(p.use) { ++*use; }
    Example_by_point& operator=(const Example_by_point &rhs);

    ~Example_by_point();
private:
    string *ps;
    int i;
    size_t *use;
};//此处要有结束分号

Example_by_point& Example_by_point::operator=(const Example_by_point &rhs)
{
    ++*rhs.use;
    if (--*use == 0)
    {
        delete ps;
        delete use;
    }
    ps = rhs.ps;
    i = rhs.i;
    use = rhs.use;
    return *this;
}

Example_by_point::~Example_by_point()
{
    if (--*use == 0)
    {
        delete ps;
        delete use;
    }
}
//对于分配了资源的类,定义swap可能是一种非常重要的优化手段!通过自定义交换指针元素,避免对象被拷贝,节省时间!
//移动构造函数,移动赋值函数。对于指针,当右值移动后须将原指针指向nullptr!定义了移动构造函数和移动赋值函数必须定义拷贝操作,否则移动函数被删除!性能优化!

int main()
{
    Example_Copy s1("123");
    Example_Copy s2("234", vector<string>{"110","120"});//直接初始化。拷贝初始化限制,单一参数的构造函数是explicit的?vector<int> v=10,错误初始化行为!
    Example_Copy s3=s2;                                 //拷贝初始化。实参传递给非引用形参,返回类型为非引用类型的函数,花括号初始化数组等元素!
    
    //c++新标准,对象移动,右值引用!右值引用只能绑定到一个将要销毁的对象!
    int t = 42;
    int &r_t = t;                   //引用只能绑定左值,常量引用可以绑定右值,右值引用可以绑定右值!
    const int &c_r_t = t * 10;
    int &&r_r_t = t * 10;
    int &&r_r_t2 = std::move(t);    //move将左值转为右值,右值引用后,必须确保不再对原对象进行操作!
    

    system("pause");
    return 0;
}

//拷贝构造函数、拷贝赋值函数、析构函数,移动构造函数、移动赋值函数!
//13.4、13.5、13.6掠过了~

相关文章

网友评论

      本文标题:chapter-13

      本文链接:https://www.haomeiwen.com/subject/nnrnlxtx.html