构造函数

按参数分类:有参构造(默认构造)和无参构造
按类型分类:普通构造和拷贝构造

构造函数定义
class Person {
  ...
  //无参构造函数
  Person(){
    ...
  }
  // 有参构造函数
  Person(int a){
    ...
  }
  // 拷贝构造函数
  Person(const Person &p){
    age = p.age;
  }
  int age;
  ...
};


构造函数的调用
// 1.括号法
Person p1;      // 默认构造函数调用
Person p2(10);  // 有参构造函数调用
Person p3(p2);  // 拷贝构造函数调用

// 2.显示法
Person p1;      // 默认构造函数调用
Person p2 = Person(10);  // 有参构造函数调用
Person p3 = Person(p2);  // 拷贝构造函数调用

// 3.隐式传递法
Person p1 = 10;  // 有参构造函数调用
Person p2 = p1;  // 拷贝构造函数调用


拷贝构造函数调用时机

C++中拷贝构造函数调用时机:

  • 使用已经创建的对象来初始化新对象
Person p1(20);
Person p2(p1);  // 拷贝构造函数调用
  • 值传递的方式给函数参数传值
void func(Person p){
  ...
}
Person p;
func(p);  // 拷贝构造函数调用
  • 值方式返回局部对象(CLion中不会调用,VS貌似可以)
Person func(){
  Person p;
  return p;
}
Person p = func();  // 拷贝构造函数调用


构造函数调用规则

默认情况下,C++编译器至少给一个类添加3个函数:

  • 默认构造函数(无参,函数体为空)
  • 默认析构函数(无参,函数体为空)
  • 默认拷贝构造函数,对属性值进行拷贝

规则:

  • 用户定义有参构造函数,C++不再提供默认无参构造函数,但会提供默认拷贝构造
class Person {
  // 有参构造函数
  Person(int a){
    ...
  }
  int age;
};
Person p1;      // false
Person p2(10);  // true
Person p3(p2); // true
  • 用户定义拷贝构造函数,C++不会再提供其他构造函数
    (也就是如果说定义了拷贝构造函数,就必须定义其他构造函数?)
class Person {
  // 拷贝构造函数
  Person(const Person &p){
    age = p.age;
  }
  int age;
};
Person p1;      // false
Person p2(10);  // false


深拷贝与浅拷贝

析构函数:将堆区开辟的数据做释放操作

  • 浅拷贝:容易导致堆区内存重复释放
class Person{
public:
    int age{};
    int *height;
    Person(){
        cout << "Default constructor" << endl;
    };
    explicit Person(int age0, int height0){
        age = age0;
        height = new int(height0);
        cout << "Parameterized constructor" << endl;
    };
    ~Person(){
        cout << "Destructor age: " << age << " height: " << *height << endl;
        if(height != nullptr){
            delete height;
            height = nullptr;
        }
    }
};

int main(){
    Person p1(20, 170);
    Person p2(p1);
    p2.age = 21;
    cout << "p1.age: " << p1.age << " p1.height: " << *p1.height << endl;
    cout << "p2.age: " << p2.age << " p1.height: " << *p2.height << endl;
    return 0;
}

输出:

Parameterized constructor
p1.age: 20 p1.height: 170
p2.age: 21 p1.height: 170
Destructor age: 21 height: 170  // p2的析构函数调用
Destructor age: 20 height: 0    // p1的析构函数调用
free(): double free detected in tcache 2  // 报错

分析:程序会报错,甚至崩溃。p1和p2都会被析构,由于p1和p2存储在栈区,因此p2 先被释放,而p1调用析构函数的时候,height对应的内存已经被释放


浅拷贝.png
  • 深拷贝
class Person{
public:
    int age{};
    int *height;
    Person(){
        cout << "Default constructor" << endl;
    };
    explicit Person(int age0, int height0){
        age = age0;
        height = new int(height0);
        cout << "Parameterized constructor" << endl;
    };
    Person(const Person &p){
        age = p.age;
        height = new int(*p.height);
        cout << "Copy constructor" << endl;
    }
    ~Person(){
        cout << "Destructor age: " << age << " height: " << *height << endl;
        if(height != nullptr){
            delete height;
            height = nullptr;
        }
    }
};

int main(){
    Person p1(20, 170);
    Person p2(p1);
    p2.age = 21;
    cout << "p1.age: " << p1.age << " p1.height: " << *p1.height << endl;
    cout << "p2.age: " << p2.age << " p1.height: " << *p2.height << endl;
    return 0;
}

输出:

Parameterized constructor
Copy constructor
p1.age: 20 p1.height: 170
p2.age: 21 p1.height: 170
Destructor age: 21 height: 170  // p2的析构函数调用
Destructor age: 20 height: 170  // p1的析构函数调用
深拷贝.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容