原型模式

[实验任务一]:向量的原型

C++完成数学中向量的封装,其中,用指针和动态申请支持向量长度的改变,使用浅克隆和深克隆复制向量类,比较这两种克隆方式的异同。

 

 #include <iostream>

#include <stdexcept>

 

class Vector {

private:

    int* elements; // 指向向量元素的指针

    size_t size;   // 当前向量中元素的数量

    size_t capacity; // 向量的最大容量

 

public:

    // 构造函数

    Vector(size_t initCapacity = 10) : size(0), capacity(initCapacity) {

        elements = new int[capacity];

    }

 

    // 析构函数

    ~Vector() {

        delete[] elements;

    }

 

    // 深拷贝构造函数

    Vector(const Vector& other) : size(other.size), capacity(other.capacity) {

        elements = new int[capacity];

        for (size_t i = 0; i < size; ++i) {

            elements[i] = other.elements[i];

        }

    }

 

    // 浅拷贝构造函数(通常不会显式地写,这里仅为了演示)

    Vector(Vector& other, bool shallow) : size(other.size), capacity(other.capacity) {

        if (shallow) {

            elements = other.elements; // 直接复制指针

        } else {

            elements = new int[capacity];

            for (size_t i = 0; i < size; ++i) {

                elements[i] = other.elements[i];

            }

        }

    }

 

    // 赋值运算符重载

    Vector& operator=(const Vector& other) {

        if (this != &other) { // 防止自赋值

            delete[] elements; // 清理已有的资源

            size = other.size;

            capacity = other.capacity;

            elements = new int[capacity];

            for (size_t i = 0; i < size; ++i) {

                elements[i] = other.elements[i];

            }

        }

        return *this;

    }

 

    // 添加元素

    void push_back(int value) {

        if (size == capacity) {

            resize(capacity * 2);

        }

        elements[size++] = value;

    }

 

    // 获取或设置元素

    int& operator[](size_t index) {

        if (index >= size) throw std::out_of_range("Index out of range");

        return elements[index];

    }

 

    const int& operator[](size_t index) const {

        if (index >= size) throw std::out_of_range("Index out of range");

        return elements[index];

    }

 

    // 改变向量大小

    void resize(size_t newCapacity) {

        int* newElements = new int[newCapacity];

        for (size_t i = 0; i < size; ++i) {

            newElements[i] = elements[i];

        }

        delete[] elements;

        elements = newElements;

        capacity = newCapacity;

    }

 

    // 获取向量大小

    size_t getSize() const {

        return size;

    }

 

    // 获取向量容量

    size_t getCapacity() const {

        return capacity;

    }

};

 

区别:

深克隆:当一个对象被复制时,不仅复制了对象本身,还复制了它所引用的所有对象。这意味着两个对象是完全独立的,修改其中一个不会影响另一个。上述代码中的拷贝构造函数默认实现了深克隆。

浅克隆:只复制对象的基本数据类型成员变量,对于引用类型的数据成员,则只是复制其引用而不复制引用的对象本身。因此,两个对象将共享同一份引用数据。这在某些情况下可能会导致未预期的行为,比如在一个对象中修改了引用的数据,会影响到另一个对象。上述代码中的第二个构造函数Vector(Vector& other, bool shallow)展示了如何实现浅克隆,但通常不会这样做,因为浅克隆容易引发问题,特别是在处理动态内存时。

 

posted @   平安喜乐×  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示