编程语言
首页 > 编程语言> > C++ 拷贝构造

C++ 拷贝构造

作者:互联网

12 拷贝构造

// Currency as pass-by-value argument
void func(Currency p)
{
    cout << "X = " << p.dollars();
}

Currency bucks(100, 0);
func(bucks);    // bucks is copied into p
#include <iostream>
#include <string>
using namespace std;

static int objectCount = 0;

class HowMany {
public:
    HowMany() { objectCount++; print("HowMany()"); }
    void print(const string& msg = "") {
        if (msg.size() != 0) {
            cout << msg << ": ";
        }
        cout << "objectCount = "
             << objectCount << endl;
    }
    ~HowMany() {
        objectCount--;
        print("~HowMany()");
    }
};

HowMany f(HowMany x) {
    cout << "begin of f " << endl;
    x.print("x argument inside f() ");
    cout << "end of f" << endl;
    return x;
}

int main() {
    HowMany h;
    h.print("after construction of h");
    HowMany h2 = f(h);
    h.print("after call to f()");
}
#include <iostream>
#include <string>
using namespace std;

static int objectCount = 0;

class HowMany {
public:
    HowMany() { objectCount++; print("HowMany()"); }
    HowMany(int i) { objectCount++; print("HowMany(int)"); }
    // 拷贝构造
    HowMany(const HowMany& o) { objectCount++; print("HowMany(HM)"); }
    void print(const string& msg = "") {
        if (msg.size() != 0) {
            cout << msg << ": ";
        }
        cout << "objectCount = "
             << objectCount << endl;
    }
    ~HowMany() {
        objectCount--;
        print("~HowMany()");
    }
};

HowMany f(HowMany x) {
    cout << "begin of f " << endl;
    x.print("x argument inside f() ");
    cout << "end of f" << endl;
    return x;
}

int main() {
    HowMany h;
    h.print("after construction of h");
    // 这种方式,是可以初始化 h2 的
    HowMany h2 = 10;
    // HowMany h2 = h;
    HowMany h3 = f(h);
    h.print("after call to f()");
}

12.1 The copy cosntructor

12.2 class contains pointers

#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;

class Person {
public:
    Person(const char *s);
    ~Person();
    void print();

// private:
    char *name;
};

Person::Person(const char *s) {
    name = new char[::strlen(s) + 1];
    ::strcpy(name, s);
}

Person::~Person() {
    delete []name;
}

int main() 
{
    Person p1("John");
    Person p2(p1);

    printf("p1.name = %p\n", p1.name);
    printf("p2.name = %p\n", p2.name);
    return 0;
}

// p1.name 和 p2.name 指向同一个地址,析构的时候,会析构两次;
// 程序运行会报错。
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;

class Person {
public:
    Person(const char *s);
    // 自定义拷贝构造
    Person(const Person& w);
    ~Person();
    void print();

// private:
    char *name;
};

Person::Person(const char *s) {
    name = new char[::strlen(s) + 1];
    ::strcpy(name, s);
}

Person::Person(const Person& w) {
    name = new char[::strlen(w.name) + 1];
    ::strcpy(name, w.name);
}

Person::~Person() {
    delete []name;
}

int main() 
{
    Person p1("John");
    Person p2(p1);

    printf("p1.name = %p\n", p1.name);
    printf("p2.name = %p\n", p2.name);
    return 0;
}

12.3 When are copy constructor called?

void roster(Person);    // 函数声明

Person child("Tom");    // 创建对象
roster(child);          // 调用函数,参数为对象本身
Person baby_a("Fred");

// 以下需要使用 copy constructor
Person baby_b = baby_a;  // not an assignment
Person baby_c(baby_a);   // not an assignment
Person f()
{
    Person player("Musk");
    return player;
}

int main()
{
    Person p = f();
    return 0;
}

12.3.1 编译器对拷贝的优化

// 示例一:
Person copy_func(char *who) {
    Person local(who);
    local.print();
    return local;   // copy constructor called!
}

// 示例二:
Person nocopy_func(char *who) {
    return Person(who); // no copy needed!
}

12.4 Construction vs. assignment

12.5 Copy constructor guidelines


参考资料:

标签:include,HowMany,name,构造,char,Person,C++,拷贝,copy
来源: https://www.cnblogs.com/linkworld/p/16382428.html