• C++ 读MTK代码 综测校准 PSU读开关电源电压或电流 visa


    为啥要使用接口?因为有多个电源,接口都相似的。再加型号上层很少改动(类型切换)或不用改。
    为啥要使用友元?友元函数的主要作用是允许外部函数或类访问被声明为友元的类的私有成员。
    如果不使用友元怎么做?最后回答。
    在这里插入图片描述

    1.C++定义dll接口

    // The following ifdef block is the standard way of creating macros which make exporting 
    // from a DLL simpler. All files within this DLL are compiled with the PSU_DLL_EXPORTS
    // symbol defined on the command line. this symbol should not be defined on any project
    // that uses this DLL. This way any other project whose source files include this file see 
    // PSU_DLL_API functions as being imported from a DLL, whereas this DLL sees symbols
    // defined with this macro as being exported.
    #pragma once
    #ifndef PSU_DLL_H
    #define PSU_DLL_H
    
    #ifdef PSU_DLL_EXPORTS
    #	define PSU_DLL_API __declspec(dllexport)
    #else
    #	define PSU_DLL_API __declspec(dllimport)
    #endif
    
    #include 
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    //
    typedef enum
    {
    	PSU_UNKONW = -1,
    	PSU_BEGIN = 0,
    	PSU_Agilent_663xx = 0,
    	PSU_Agilent_E3631A,
    	PSU_GWINSTTEK_PPH1503D,
    	PSU_END
    } PSU_TYPE;
    
    typedef enum
    {
    	PSU_OFF = 0,
    	PSU_ON,
    } PSU_ONOFF;
    
    //
    // PSU Interface
    class CPSUInterface
    {
    	friend PSU_DLL_API CPSUInterface* PSUCreate(const char* pcPsuGpibAddr, PSU_TYPE ePsuType);
    protected:
    	CPSUInterface();
    public:
    	virtual ~CPSUInterface(void);
    
    	virtual int PSU_Initialize(double Voltage_Limit, double Current_Limit) = 0;
    	virtual int PSU_PowerOnOff(PSU_ONOFF OnOffState) = 0;
    	virtual int PSU_Set_VoltCurr(double Target_Voltage, double Target_Current) = 0;
    	virtual int PSU_Voltage_Measure(double *Voltage_Result, double *Current_Result) = 0;
    	virtual int PSU_DeInitial() = 0;
    };
    
    // Can't construct directly, use this, delete the pointer if no longer use
    PSU_DLL_API CPSUInterface* PSUCreate(const char* pcPsuGpibAddr, PSU_TYPE ePsuType);
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif // #ifndef PSU_DLL_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    2.导出接口。导出友元函数。定义基类继承接口类,存放一些通用的变量值,如地址,message等。具体类继承基类。

    // PSU_DLL.cpp : Defines the exported functions for the DLL application.
    //
    
    #include "stdafx.h"
    #include "PSU_DLL.h"
    
    #include "AG663XX.h"
    #include "AG3631A.h"
    #include "GW1503D.h"
    
    
    CPSUInterface::CPSUInterface()
    {
    }
    
    CPSUInterface::~CPSUInterface(void)
    {
    }
    
    
    CPSUInterface* PSUCreate(const char* pcPsuGpibAddr, PSU_TYPE ePsuType)
    {
    	switch (ePsuType)
    	{
    	case PSU_Agilent_663xx:
    		return new CAG663XX(pcPsuGpibAddr, ePsuType);
    	case PSU_Agilent_E3631A:
    		return new CAG3631A(pcPsuGpibAddr, ePsuType);
    	case PSU_GWINSTTEK_PPH1503D:
    		return new GW1503D(pcPsuGpibAddr, ePsuType);
    	default:
    		return NULL;
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    
    #include "stdafx.h"
    #include "PSUBasic.h"
    
    
    CPSUBasic::CPSUBasic(const char* pcPsuGpibAddr,PSU_TYPE ePsuType )
    {
    	strcpy_s(m_cPsuGpibAddr, pcPsuGpibAddr);
    	m_ePsuType = ePsuType;
    	m_bPsuInitFlag = false;
    	m_vsPsuHandle = NULL;
    	memset(m_cErrorMSG, 0, sizeof(m_cErrorMSG));
    }
    
    CPSUBasic::~CPSUBasic(void)
    {
    }
    
    void CPSUBasic::PSU_GetErrorMsg( char* pcErrorMSG, int iMsgSize)
    {
    	if (iMsgSize > 100)
    		iMsgSize = 100;
    	strcpy_s(pcErrorMSG, iMsgSize, m_cErrorMSG);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    3.友元函数切换多态 返回句柄指针

    在这里插入图片描述

    4.句柄指针调用具体类

    在这里插入图片描述
    在这里插入图片描述

    5.删除指针。

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述

    如果不使用友元函数,你可以通过提供公共的成员函数或访问器函数来实现对类的私有成员的访问。这可以帮助维持封装性,而无需将外部函数或类声明为友元。以下是一些方法:

    1. 公共成员函数:在类中提供公共成员函数,这些函数可以用于修改或访问私有成员。这是最常见的方式,遵循了封装的概念。

      class MyClass {
      private:
          int privateData;
      
      public:
          int getPrivateData() {
              return privateData;
          }
      
          void setPrivateData(int value) {
              privateData = value;
          }
      };
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13

      在这个示例中,getPrivateDatasetPrivateData 是用于获取和设置私有成员 privateData 的公共成员函数。

    2. 访问器函数:如果需要更细粒度的控制,你可以使用访问器函数,这些函数可以用于获取和设置私有成员,同时允许你添加额外的逻辑。

      class MyClass {
      private:
          int privateData;
      
      public:
          int getPrivateData() {
              // 可以在这里添加额外的逻辑
              return privateData;
          }
      
          void setPrivateData(int value) {
              // 可以在这里添加额外的逻辑
              privateData = value;
          }
      };
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15

      这种方法允许你在访问私有成员时执行自定义操作,例如范围检查或日志记录

    使用公共成员函数或访问器函数的好处是,它们保持了封装性,同时提供了对私有成员的受控访问,而不需要使用友元函数。友元函数通常是在需要更特殊的访问权限或操作时使用的,但应谨慎使用以保持良好的封装性。

  • 相关阅读:
    Java方法的重载/方法的内存/基本数据类型与引用数据类型/方法的值传递
    算法分享三个方面学习方法(做题经验,代码编写经验,比赛经验)
    JVM虚拟机:运行时数据区详解
    cobalt strike 的基础使用
    java计算机毕业设计社区物品交易系统源码+系统+数据库+lw文档+mybatis+运行部署
    期末复习总结【MySQL】五种约束类型, 主键和外键的使用方式(重点)
    哈希的使用
    [mit6.1810] Lab system calls
    RabbitMQ(消息队列)
    基于深度学习的菠萝与果叶视觉识别及切断机构设计
  • 原文地址:https://blog.csdn.net/chenhao0568/article/details/133983818