C++ 定义一个类,类的成员变量包含指针。示例2 DsBytes

编程环境:win10,vs2017

DsBytes类封装了一个unsigned char *指针和一个记录数据长度的unsigned int。

实现了仿函数、转换函数和move

代码

#pragma once

 

#include <iostream>

 

namespace ds09 {

   

    using namespace std;

    typedef unsigned char Byte;

 

    class DsBytes

    {

    private:

         Byte * data;

         size_t length;

 

    private:

         void Copy(const DsBytes& bytes)

         {

             this->data = new Byte[bytes.length];  //动态申请的堆内存,用完了,要释放。

             memcpy(this->data, bytes.data, bytes.length);

             this->length = bytes.length;

         }

 

         void Move(DsBytes& bytes)

         {

             this->data = bytes.data;

             this->length = bytes.length;

 

             bytes.data = nullptr;

             bytes.length = 0;

         }

 

         void Destroy()

         {

             if (data != nullptr)

             {

                  delete[] data;  //必须显示析构数组,删除数组时,[]不能漏掉,否则会造成内存泄漏

             }

         }

 

    public//构造,析构

         DsBytes():data(nullptr), length(0) {}

         DsBytes(size_t length,Byte value = 0) :length(length)

         {

             this->data = new Byte[length];

             memset(this->data, value, length);

         }

 

         DsBytes(const DsBytes& bytes)

         {

             Copy(bytes);

         }

 

         DsBytes operator = (const DsBytes& bytes)

         {

             if (this != &bytes//dd和this不是同一个对象

             {

                  Destroy();  //删除旧的data

 

                  Copy(bytes);;  //建立新的data

             }

             return *this;

         }

 

         DsBytes(DsBytes&& bytes)

         {

             Move(bytes);

         }

 

         DsBytes operator = (DsBytes&& bytes)

         {

             if (this != &bytes//dd和this不是同一个对象

             {

                  Move(bytes);

             }

             return *this;

         }

 

         ~DsBytes()

         {

             Destroy();

         }

 

    public//Get

         Byte * GetData() const

         {

             return this->data;

         }

 

         const size_t& GetLength() const

         {

             return this->length;

         }

 

    public:

         //转换函数 conversion function

         //计算校验和

         operator Byte() const

         {

             Byte b = 0;

             for (int i = 0;i<length;++i)

             {

                  b += *(data + i);

             }

             return b;

         }

 

         //仿函数(function-like classes)

         //将数据写到外部缓冲区,或者从外部缓冲区读数据

         //externalBytes : 外部缓冲区地址

         //externalOffset : 外部缓冲区地址偏移量

         //internalOffet : 内部缓冲区地址偏移量

         //size :读写数据的长度

         //isRead : true=从外部缓冲区读数据. false=将数据写到外部缓冲区

         //

         void operator () (Byte * externalBytes,const size_t& externalOffset,const size_t& internalOffet,const size_t& size, bool isRead = false) const

         //仿函数定义 : 第一个()是调用运算符, 此运算符被重载。第二个()是定义参数。

         {

             if (isRead)

             {

                  //从外部缓冲区读数据

                  memcpy(data + internalOffet, externalBytes + externalOffset, size);

             }

             else

             {

                  //将数据写到外部缓冲区

                  memcpy(externalBytes + externalOffset, data + internalOffet, size);

             }

         }

 

         void operator () (DsBytes& db, const size_t& externalOffset, const size_t& internalOffet, const size_t& size, bool isRead = false) const

         //仿函数定义 : 第一个()是调用运算符, 此运算符被重载。第二个()是定义参数。

         {

             if (isRead)

             {

                  //从外部缓冲区读数据

                  memcpy(data + internalOffet, db.GetData() + externalOffset, size);

             }

             else

             {

                  //将数据写到外部缓冲区

                  memcpy(db.GetData() + externalOffset, data + internalOffet, size);

             }

         }

 

         Byte operator + (const DsBytes& db) const

         {

             Byte a = *this;

             Byte b = db;

             return (Byte)(a + b);

         }

 

         Byte operator - (const DsBytes& db) const

         {

             Byte a = *this;

             Byte b = db;

             return (Byte)(a - b);

         }

    };

 

    //重载运算符 << ,不能在类的内部定义,只能放在类的外部定义。

    std::ostream& operator << (std::ostream& out, const DsBytes& db)

    {

         if (&db != nullptr)

         {

             const Byte * b = db.GetData();

             const Byte * e = b + db.GetLength();

 

             char buffer1[100];

             snprintf(buffer1, 100, "Address=0x%08x  data=address:0x%08x length:%d value:", &db, b, db.GetLength());

             out << buffer1;

 

             char buffer[10];

             for (const Byte * i = b; i != e; ++i)

             {

                  snprintf(buffer, 10, "0x%02x", (Byte)*i);

                  out << buffer << " ";

             }

         }

         return out;

    }

 

    //测试代码

    void testDsData()

    {

         DsBytes bytes01(5, 0xff);

         cout << "bytes01 : " << bytes01 << endl;

 

         DsBytes bytes02(move(bytes01));

         cout << "将 bytes01 move 到 bytes02后的结果:" << endl;

         cout << "bytes01 : " << bytes01 << endl;

         cout << "bytes02 : " << bytes02 << endl;

 

         DsBytes bytes03(3, 0xaa);

         bytes02(bytes03, 0, 1, bytes03.GetLength(),true);

         cout << "仿函数将 bytes03 复制到 bytes02后的结果:" << endl;

         cout << "bytes03 : " << bytes03 << endl;

         cout << "bytes02 : " << bytes02 << endl;

 

         Byte crc = bytes02;

         cout << "转换函数 bytes02 bytes03 的校验和:" << endl;

         cout << "bytes02 crc : " << hex << "0x" << (int)crc << endl;

         cout << "bytes03 crc : " << hex << "0x" << (int)bytes03 << endl;

 

         cout << "bytes03 + bytes02 = " << hex << "0x" << (int)(bytes03 + bytes02) << endl;

         cout << "bytes03 - bytes02 = " << hex << "0x" << (int)(bytes03 - bytes02) << endl;

    }

 

}

测试代码的输出:

bytes01 : Address=0x00b3fdd8  data=address:0x00bdf130 length:5 value:0xff 0xff 0xff 0xff 0xff

将 bytes01 move 到 bytes02后的结果:

bytes01 : Address=0x00b3fdd8  data=address:0x00000000 length:0 value:

bytes02 : Address=0x00b3fdc8  data=address:0x00bdf130 length:5 value:0xff 0xff 0xff 0xff 0xff

仿函数将 bytes03 复制到 bytes02后的结果:

bytes03 : Address=0x00b3fdb8  data=address:0x00bdb168 length:3 value:0xaa 0xaa 0xaa

bytes02 : Address=0x00b3fdc8  data=address:0x00bdf130 length:5 value:0xff 0xaa 0xaa 0xaa 0xff

转换函数 bytes02 bytes03 的校验和:

bytes02 crc : 0xfc

bytes03 crc : 0xfe

bytes03 + bytes02 = 0xfa

bytes03 - bytes02 = 0x2

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值