如何实现对象的克隆?如何实现单例模式?

实现对对象的克隆

移动构造(移动语义实现的):通过移动语义实现移动构造,旧对象的资源的拥有权转移给新对象

拷贝构造 :通过旧对象初始化新对象,可能会存在浅拷贝和深拷贝的问题

赋值运算符:把一个对象的属性赋值给另一个对象,实现对前一个对象的拷贝

自定义一个clone方法:对于基类和派生类的层次结构,定义一个虚函数 clone(),在派生类中重写该函数,以确保多态克隆。

cpp 复制代码
#include <iostream>
#include <string>

class Base {
public:
    virtual ~Base() {}
    virtual Base* clone() const = 0;
    virtual void display() const = 0;
};


class Derived : public Base {
private:
    int value;
    std::string name;

public:
    Derived(int v = 0, const std::string& n = "") : value(v), name(n) {}

    // 实现克隆方法
    Base* clone() const override {
        return new Derived(*this);
    }

    void display() const override {
        std::cout << "Value: " << value << ", Name: " << name << std::endl;
    }
};


int main() {
    Derived obj1(42, "Derived Object");
    std::cout << "Original object: ";
    obj1.display();

    // 使用 clone 方法进行克隆
    Base* obj2 = obj1.clone();
    std::cout << "Cloned object: ";
    obj2->display();

    delete obj2;

    return 0;
}

使用模板和完美转发:使用模板函数进行克隆,通过完美转发将参数传递给对象的构造函数,实现通用的克隆机制。

完美转发:在 C++ 中,完美转发(Perfect Forwarding)是一种将参数以其原始的类型和值类别(左值或右值)传递给另一个函数的技术。它主要使用 std::forward 函数模板和转发引用(也称为万能引用,Forwarding Reference)来实现。

cpp 复制代码
#include <iostream>
#include <utility>

template <typename T, typename... Args>
T* clone(Args&&... args) {
    return new T(std::forward<Args>(args)...);
}


class SimpleClass {
private:
    int value;

public:
    SimpleClass(int v = 0) : value(v) {}

    void display() const {
        std::cout << "Value: " << value << std::endl;
    }
};


int main() {
    SimpleClass obj1(10);
    std::cout << "Original object: ";
    obj1.display();

    // 使用模板函数进行克隆
    SimpleClass* obj2 = clone<SimpleClass>(obj1);
    std::cout << "Cloned object: ";
    obj2->display();

    delete obj2;

    return 0;
}

如何实现单例模式?

懒汉式(线程不安全)

cpp 复制代码
#include <iostream>


class Singleton {
private:
    static Singleton* instance;
    Singleton() {}  // 构造函数私有,防止外部实例化

public:
    static Singleton* getInstance() {
        if (instance == nullptr) {
            instance = new Singleton();
        }
        return instance;
    }


    void display() const {
        std::cout << "This is a singleton instance." << std::endl;
    }
};


Singleton* Singleton::instance = nullptr;


int main() {
    Singleton* s1 = Singleton::getInstance();
    Singleton* s2 = Singleton::getInstance();
    s1->display();
    s2->display();
    // 注意:没有释放内存,可能导致内存泄漏
    return 0;
}

懒汉式(线程安全,使用互斥锁)

cpp 复制代码
#include <iostream>
#include <mutex>


class Singleton {
private:
    static Singleton* instance;
    static std::mutex mtx;
    Singleton() {}  // 构造函数私有,防止外部实例化

public:
    static Singleton* getInstance() {
        std::lock_guard<std::mutex> lock(mtx);
        if (instance == nullptr) {
            instance = new Singleton();
        }
        return instance;
    }


    void display() const {
        std::cout << "This is a singleton instance." << std::endl;
    }
};


Singleton* Singleton::instance = nullptr;
std::mutex Singleton::mtx;


int main() {
    Singleton* s1 = Singleton::getInstance();
    Singleton* s2 = Singleton::getInstance();
    s1->display();
    s2->display();
    // 注意:没有释放内存,可能导致内存泄漏
    return 0;
}

饿汉式

cpp 复制代码
#include <iostream>


class Singleton {
private:
    static Singleton* instance;
    Singleton() {}  // 构造函数私有,防止外部实例化

public:
    static Singleton* getInstance() {
        return instance;
    }


    void display() const {
        std::cout << "This is a singleton instance." << std::endl;
    }
};


Singleton* Singleton::instance = new Singleton();


int main() {
    Singleton* s1 = Singleton::getInstance();
    Singleton* s2 = Singleton::getInstance();
    s1->display();
    s2->display();
    // 注意:没有释放内存,可能导致内存泄漏
    return 0;
}

Meyers' Singleton (C++11 及以后)

cpp 复制代码
#include <iostream>


class Singleton {
private:
    Singleton() {}  // 构造函数私有,防止外部实例化


public:
    static Singleton& getInstance() {
        static Singleton instance;
        return instance;
    }


    void display() const {
        std::cout << "This is a singleton instance." << std::endl;
    }
};


int main() {
    Singleton& s1 = Singleton::getInstance();
    Singleton& s2 = Singleton::getInstance();
    s1.display();
    s2.display();
    return 0;
}
相关推荐
不正经学生1 小时前
C语言大小端字节序:内存里字节的排列顺序
c语言·开发语言·arm开发·c++·算法·c#
小则又沐风a2 小时前
负载均衡式在线OJ---------第二幕
linux·c++·后端
a3535413822 小时前
C++项目如何架构优化
java·c++·架构
键盘会跳舞2 小时前
C++:智能指针源码级深度拆解——从 RAII 思想到引用计数的内存安全体系
c++·智能指针·unique_ptr·shared_ptr·weak_ptr
SNAKEpc121382 小时前
OpenGL(十三)- Mip贴图
c语言·c++·图形渲染·贴图
会周易的程序员15 小时前
aiDgePLC — IEC61131-3 ST PLC 虚拟机调试与运行环境
c++·物联网·虚拟机·st·工业协议·iec61131·梯形图
wuminyu17 小时前
JVM利用io_uring优化堆外内存性能原理剖析
java·linux·c语言·jvm·c++
gumichef17 小时前
第二章:C/C++内存管理
c++·内存管理
小龙报20 小时前
【优选算法】1.搜索插入位置 2.x的平方根
java·c语言·数据结构·c++·python·算法·蓝桥杯
A_humble_scholar20 小时前
Linux(二) C++ TCP/UDP网络编程
linux·网络·c++