C++ 语言特性10 - 委托构造函数

1:什么是委托构造函数?

在C++中,委托构造函数(Delegating Constructor)是一种特殊的构造函数,它在构造函数的初始化列表中调用同一个类中的另一个构造函数,从而实现代码的复用。这种特性在C++11中引入。

cpp 复制代码
class MyClass {
public:
    // 委托构造函数
    MyClass(int x, int y) : MyClass(x) {
        // 委托给另一个构造函数
    }
    
    // 被委托的构造函数
    MyClass(int x) {
        // 初始化代码
    }
};

2. 委托构造函数在什么场景中使用?

  • 代码复用: 当多个构造函数需要执行相同的初始化步骤时,可以使用委托构造函数来避免代码重复。
cpp 复制代码
#include <iostream>
#include <string>

class Person {
public:
    Person() : Person("Unknown", 0) {
        std::cout << "Default constructor called\n";
    }
    
    Person(std::string name) : Person(name, 0) {
        std::cout << "Name constructor called\n";
    }
    
    Person(std::string name, int age) : name(name), age(age) {
        std::cout << "Full constructor called for " << name << std::endl;
    }

private:
    std::string name;
    int age;
};

int main() {
    Person p1;
    Person p2("Alice");
    Person p3("Bob", 30);
    return 0;
}
  • 链式委托: 一个构造函数可以委托给另一个构造函数,而后者又可以委托给下一个,形成一个委托链。
cpp 复制代码
class Matrix {
public:
    Matrix() : Matrix(0, nullptr) {
        std::cout << "Default constructor called\n";
    }
    
    Matrix(size_t rows, size_t cols) : Matrix(rows, cols, new double*[rows]) {
        std::cout << "Size constructor called\n";
    }
    
    Matrix(size_t rows, size_t cols, double** data) : rows(rows), cols(cols), data(data) {
        std::cout << "Data constructor called\n";
    }

private:
    size_t rows;
    size_t cols;
    double** data;
};

int main() {
    Matrix mat;
    return 0;
}
  • 继承和覆盖: 在继承的类中,可以利用委托构造函数调用基类的构造函数,以确保正确的初始化。
cpp 复制代码
class Base {
public:
    Base(int x) {
        std::cout << "Base constructor with int called\n";
    }
};

class Derived : public Base {
public:
    // 委托给基类的构造函数
    Derived(double x) : Base(static_cast<int>(x)) {
        std::cout << "Derived constructor with double called\n";
    }
};

int main() {
    Derived d(3.14);
    return 0;
}

3. 注意事项:

  • 委托目标的选择: 确保委托的目标构造函数是有效的,并且能够正确初始化对象。
cpp 复制代码
#include <iostream>
#include <string>
#include <vector>

class Data {
public:
    // 正确的委托目标选择
    Data(const std::string& str) : Data(str.begin(), str.end()) {}
    
    // 正确的初始化列表
    Data(const char* begin, const char* end) {
        // 做一些初始化工作
        for (; begin != end; ++begin) {
            values.push_back(*begin);
        }
        std::cout << "Data initialized with " << values.size() << " elements." << std::endl;
    }

private:
    std::vector<char> values;
};

int main() {
    Data d("Hello");
    return 0;
}
  • 构造函数的递归调用: 避免构造函数之间的无限递归调用。
cpp 复制代码
class RecursiveConstructor {
public:
    // 错误:递归调用自身
    RecursiveConstructor() : RecursiveConstructor(42) {}
    
    RecursiveConstructor(int) {
        std::cout << "Constructor called" << std::endl;
    }
};

int main() {
    // RecursiveConstructor rc; // 这将导致无限递归调用
    return 0;
}
  • 资源管理: 在委托构造函数中,确保资源被正确管理和释放。
cpp 复制代码
#include <iostream>

class Resource {
public:
    // 委托构造函数正确地管理资源
    Resource(int id) : Resource(id, new int(id * 10)) {
        std::cout << "Resource ID: " << *data << std::endl;
    }
    
    // 被委托的构造函数
    Resource(int id, int* data) : id(id), data(data) {}
    
    ~Resource() {
        delete data;
    }

private:
    int id;
    int* data;
};

int main() {
    Resource res(1);
    return 0;
}

//在这个例子中,委托构造函数确保了资源(动态分配的整数)被正确地分配和初始化。
  • 异常安全: 委托构造函数需要考虑异常安全性,确保在抛出异常时对象处于一致的状态。
cpp 复制代码
#include <iostream>
#include <string>

class ExceptionSafe {
public:
    // 委托构造函数
    ExceptionSafe(const std::string& str) : ExceptionSafe(str.begin(), str.end()) {
        // 做一些可能会抛出异常的操作
        if (str.empty()) {
            throw std::invalid_argument("String is empty");
        }
    }
    
    // 被委托的构造函数
    ExceptionSafe(const char* begin, const char* end) {
        // 做一些初始化工作
        try {
            for (; begin != end; ++begin) {
                values.push_back(*begin);
            }
        } catch (...) {
            // 异常发生时进行清理工作
            std::cout << "Exception caught during initialization." << std::endl;
            throw;
        }
        std::cout << "Data initialized with " << values.size() << " elements." << std::endl;
    }

private:
    std::vector<char> values;
};

int main() {
    try {
        ExceptionSafe es("Hello");
        ExceptionSafe esEmpty("");
    } catch (const std::exception& e) {
        std::cout << "Caught exception: " << e.what() << std::endl;
    }
    return 0;
}
相关推荐
黑听人2 分钟前
【力扣 简单 C】141. 环形链表
c语言·开发语言·数据结构·算法·leetcode
谷雨不太卷21 分钟前
AVL树的实现
数据结构·c++·算法
Blossom.1181 小时前
基于深度学习的智能图像分类系统:从零开始构建
开发语言·人工智能·python·深度学习·神经网络·机器学习·分类
缘友一世1 小时前
java设计模式[2]之创建型模式
java·开发语言·设计模式
cyc&阿灿2 小时前
Java中extends与implements深度解析:继承与接口实现的本质区别
java·开发语言
别来无恙1493 小时前
岛屿周长问题的三种解法:直接计数法、数学计算法与深度优先搜索
java·c++·算法·深度优先·dfs
liujing102329294 小时前
Day13_C语言基础&项目实战
c语言·开发语言
周振超的4 小时前
c++编译第三方项目报错# pragma warning( disable: 4273)
开发语言·c++
JH30735 小时前
Java Stream API 在企业开发中的实战心得:高效、优雅的数据处理
java·开发语言·oracle
愚润求学7 小时前
【递归、搜索与回溯】FloodFill算法(一)
c++·算法·leetcode