C++类的使用(1)

#include <iostream>
#include <algorithm> // 用于 std::fill 和 std::copy

class MyClass
{
private:
    int *data;   // 指向动态分配的内存
    size_t size; // 数据大小

public:
    // 默认构造函数
    MyClass() : data(nullptr), size(0)
    {
        std::cout << "Default Constructor called." << std::endl;
    }

    // 带参构造函数
    MyClass(size_t size, int value) : size(size)
    {
        std::cout << "Parameterized Constructor called." << std::endl;
        data = new int[size]; // new 数据类型如int int[5]... 可使用列表初始化:new int[5]{1,2,3,4,5}
        // new int{1}和new int(1)差不多 但是更推荐前者
        std::fill(data, data + size, value);
        // memset(data, value, size * sizeof(int)); // 另一种初始化方式,但更推荐使用std::fill
        // 因为memset/cpy/set是按照字节进行
    }

    // 拷贝构造函数
    MyClass(const MyClass &other) : size(other.size)
    {   //不可使用const Myclass other 因为回一直递归下去
        std::cout << "Copy Constructor called." << std::endl;
        data = new int[size];
        std::copy(other.data, other.data + size, data); // 深拷贝
    }

    // 移动构造函数
    MyClass(MyClass &&other) noexcept : data(other.data), size(other.size)
    {
        std::cout << "Move Constructor called." << std::endl;
        other.data = nullptr; // 防止析构时释放
        other.size = 0;
    }

    // 拷贝赋值运算符
    MyClass &operator=(const MyClass &other)
    {
        std::cout << "Copy Assignment Operator called." << std::endl;
        if (this != &other)
        {                                       // 自我赋值检查
            int *newData = new int[other.size]; // 先分配新内存,避免出错时内存泄漏
            std::copy(other.data, other.data + other.size, newData);
            delete[] data; // 释放旧内存
            data = newData;
            size = other.size;
        }
        return *this;
    }

    // 移动赋值运算符
    MyClass &operator=(MyClass &&other) noexcept
    {
        std::cout << "Move Assignment Operator called." << std::endl;
        if (this != &other)
        {                  // 自我赋值检查
            delete[] data; // 释放当前对象的内存
            data = other.data;
            size = other.size;
            other.data = nullptr; // 防止析构时释放
            other.size = 0;
        }
        return *this;
    }

    // 析构函数
    ~MyClass()
    {
        std::cout << "Destructor called." << std::endl;
        delete[] data; // 释放动态分配的内存
    }

    // 示例成员函数
    void print() const
    {
        std::cout << "Size: " << size << ", Data: ";
        if (data)
        {
            for (size_t i = 0; i < size; ++i)
            {
                std::cout << data[i] << " ";
            }
        }
        else
        {
            std::cout << "(null)";
        }
        std::cout << std::endl;
    }

    // 修改数据的成员函数
    void modifyData(size_t index, int value)
    {
        if (data && index < size)
        {
            data[index] = value;
        }
        else
        {
            std::cerr << "Index out of bounds or data is null." << std::endl;
        }
    }
};

int main()
{
    // 默认构造
    std::cout << "=== Default Constructor ===" << std::endl;
    MyClass a{};
    a.print();

    // 带参构造
    std::cout << "\n=== Parameterized Constructor ===" << std::endl;
    MyClass b{5, 10};
    b.print();

    // 拷贝构造
    std::cout << "\n=== Copy Constructor ===" << std::endl;
    MyClass c = b;
    c.print();

    // 移动构造
    std::cout << "\n=== Move Constructor ===" << std::endl;
    MyClass d = std::move(b);
    d.print();
    b.print(); // b 的数据已被移动,应为空

    // 拷贝赋值
    std::cout << "\n=== Copy Assignment Operator ===" << std::endl;
    MyClass e;
    e = c;
    e.print();

    // 移动赋值
    std::cout << "\n=== Move Assignment Operator ===" << std::endl;
    MyClass f;
    f = std::move(d);
    f.print();
    d.print(); // d 的数据已被移动,应为空

    // 修改数据
    std::cout << "\n=== Modify Data ===" << std::endl;
    f.modifyData(2, 99);
    f.print();

    // 析构
    std::cout << "\n=== Destructor ===" << std::endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值