栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

memcpy c++结构体运算符重载

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

memcpy c++结构体运算符重载

 memcpy是拷贝一段内存地址,在一些和设备打交道的程序中使用的较多,和memset配合使用,特别需要注意“”,如果字符数组转成字符串,而字符串是以""作为结尾符,如果使用错误会造成:

1.越界,取到脏数据,或者下一块本不该属于它的内存地址;

2.没有取到预想的数据;

 重载等于号运算符时,特别是注意空和空的对比是能够匹配到的,所以有些遍历在写完后要做单元测试或者打桩测试,避免任何意外的情况发生(比如别人传了个空指针,而自己没有做空指针判断,很可能造成程序崩溃);

下面贴一个简单的qt控制台应用程序做说明:

#-------------------------------------------------
#
# Project created by QtCreator 2020-12-11T21:28:39
#
#-------------------------------------------------

QT       += core

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Demo
TEMPLATE = app


SOURCES += main.cpp

HEADERS  += CGlobal.h
#ifndef GLOBAL_H
#define GLOBAL_H

#include 
using namespace std;

struct DEVICE_INFO
{
    int index = 0;
    string name = {};
    string strVarUUID = {};
    string strTaskUUID = {};

    DEVICE_INFO(){
        index = 0;
        name = {};
        strVarUUID = {};
        strTaskUUID = {};
    }

    bool operator==(const DEVICE_INFO &other) const {
        if ((this->strTaskUUID == other.strTaskUUID) && (this->strVarUUID == other.strVarUUID))
            return true;
        else
            return false;
    }

    void operator=(const DEVICE_INFO &other) {
        this->index = other.index;
        this->name = other.name;
        this->strVarUUID = other.strVarUUID;
        this->strTaskUUID = other.strTaskUUID;
    }
};

#endif
#include 
#include 

#include "CGlobal.h"

#include 
#include 
#include     //使用find()函数要包含该头文件
using  namespace  std;

using namespace std;

#define FILENAME_LEN 10

void * my_memcpy(void *dst,const void *src,int count)
{
    void * ret = dst;
    while (count--)
    {
        *(char *)dst = *(char *)src;
        dst = (char *)dst + 1;
        src = (char *)src + 1;
    }
    return(ret);
}

int main(int argc, char*argv[])
{
    QApplication app(argc, argv);
    string strName = "times12_times12_times12_times12_";
    char cName[FILENAME_LEN]={};
    memset(cName, 0, FILENAME_LEN);
    memcpy(cName, strName.c_str(), strName.length() > FILENAME_LEN ? FILENAME_LEN - 1 : strName.length());
    // my_memcpy(cName, strName.c_str(), strName.length() > FILENAME_LEN ? FILENAME_LEN : strName.length());
    qInfo() << cName;
    
    DEVICE_INFO device1;
    device1.index = 1;
    device1.name = "shidian";
    device1.strTaskUUID = "123";
    device1.strTaskUUID = "456";

    DEVICE_INFO device2;
    device2.index = 2;
    device2.name = "UPS";
    device2.strTaskUUID = "123";
    device2.strTaskUUID = "456";

    if (device1 == device2)
        qInfo() << "equal";
    else
        qInfo() << "not equal";

    DEVICE_INFO device3;
    device2.index = 3;
    device2.name = "KGDY";

    DEVICE_INFO device4;
    device2.index = 4;
    device2.name = "Battery";

    //特别注意(空等于空,还能相等,这在有些判断的时候特别重要)
    if (device3 == device4)
        qInfo() << "equal";
    else
        qInfo() << "not equal";

    
    vector num_list = { 2,4,6,8,10,12 };
    int find_num = 11;    //要查找的元素,类型要与vector中的元素类型一致
    vector::iterator num = find(num_list.begin(), num_list.end(), find_num);  //返回一个迭代器指针

    if (num != num_list.end())   //查找成功
    {
        qInfo() << "元素 " << *num << " 在num_list中"< 

 

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1015969.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号