当前位置: 首页 > news >正文

【CI130x 离在线】 C++一个类中调用另一个类的方法

有几种解决方案,根据你的具体需求选择:

方案1:传递CISerialPort对象引用(推荐)

在需要使用CheckConnection()的类中持有CISerialPort的引用:

cpp
// 在需要使用CheckConnection的类中
class MyOtherClass {
private:CISerialPort& serial_port_;  // 引用public:// 通过构造函数传入MyOtherClass(CISerialPort& serial_port) : serial_port_(serial_port) {}void SomeMethod() {if (serial_port_.CheckConnection()) {// 串口连接正常std::cout << "串口连接正常" << std::endl;} else {// 串口连接异常std::cout << "串口连接异常" << std::endl;}}
};// 使用示例
CISerialPort serial;
serial.Initialize("/dev/ttyUSB0", 115200);MyOtherClass other_class(serial);  // 传入串口对象
other_class.SomeMethod();

方案2:传递CISerialPort指针

cpp
class MyOtherClass {
private:CISerialPort* serial_port_;  // 指针public:MyOtherClass(CISerialPort* serial_port) : serial_port_(serial_port) {}void SomeMethod() {if (serial_port_ && serial_port_->CheckConnection()) {// 串口连接正常}}
};// 使用示例
CISerialPort serial;
MyOtherClass other_class(&serial);
other_class.SomeMethod();

方案3:使用单例模式(如果合适)

如果整个系统只需要一个串口实例:

cpp
// 在CISerialPort.h中修改
class CISerialPort {
private:static CISerialPort* instance_;  // 单例实例public:static CISerialPort* GetInstance() {if (!instance_) {instance_ = new CISerialPort();}return instance_;}// 原有方法保持不变bool CheckConnection();bool IsConnected() const;
};// 在CISerialPort.cpp中
CISerialPort* CISerialPort::instance_ = nullptr;// 在其他类中使用
class MyOtherClass {
public:void SomeMethod() {CISerialPort* serial = CISerialPort::GetInstance();if (serial->CheckConnection()) {// 使用串口}}
};

方案4:使用全局变量(简单但不推荐)

cpp
// 全局声明
extern CISerialPort g_serial_port;// 在其他类中使用
class MyOtherClass {
public:void SomeMethod() {if (g_serial_port.CheckConnection()) {// 使用串口}}
};

完整示例(推荐方案1)

MyOtherClass.h

cpp
#include "ci_serial_port.h"class MyOtherClass {
private:CISerialPort& serial_port_;public:MyOtherClass(CISerialPort& serial_port);void CheckAndUseSerial();void ProcessData();
};

MyOtherClass.cpp

cpp
#include "MyOtherClass.h"MyOtherClass::MyOtherClass(CISerialPort& serial_port) : serial_port_(serial_port) {
}void MyOtherClass::CheckAndUseSerial() {// 检查连接状态if (!serial_port_.CheckConnection()) {std::cerr << "串口连接异常,无法处理数据" << std::endl;return;}// 连接正常,继续处理ProcessData();
}void MyOtherClass::ProcessData() {// 读取串口数据auto data = serial_port_.ReadFromSerial();if (!data.empty()) {// 处理数据...}
}

main.cpp

cpp
#include "ci_serial_port.h"
#include "MyOtherClass.h"int main() {// 创建串口对象CISerialPort serial_port;// 初始化串口if (!serial_port.Initialize("/dev/ttyUSB0", 115200)) {return -1;}// 创建其他类对象并传入串口引用MyOtherClass processor(serial_port);// 使用while (true) {processor.CheckAndUseSerial();usleep(100000); // 100ms}return 0;
}

建议

  • 推荐方案1(传递引用):最安全,面向对象,易于测试

  • 如果多个地方需要访问同一个串口,考虑方案3(单例)

  • 避免方案4(全局变量),不利于代码维护和测试

选择哪种方案取决于你的具体架构设计。

http://www.zskr.cn/news/56221.html

相关文章:

  • ERP/MES与QMS的协同价值:为什么企业需要专业质量管理系统的深度解析
  • 详细介绍:数据结构八大排序:堆排序-从二叉树到堆排序实现
  • 2025 年 11 月机床厂家推荐排行榜,数控机床,智能数控机床,双头对接机床,6150机床,线轨机床,硬轨机床,重型机床公司推荐
  • 2025 年 11 月硬轨机床厂家推荐排行榜:高刚性硬轨机床,重切削硬轨机床,精密硬轨机床,数控硬轨机床公司推荐
  • QMS软件评估:从制造企业实践看标准产品的多维评估框架
  • 2025AI直播服务公司品质榜TOP5:智能交互技术,高清画质工艺解析
  • 2025 年 11 月线轨机床厂家推荐排行榜,精密线轨机床,高速线轨机床,数控线轨机床,重型线轨机床公司推荐
  • 自指生产力性格,自洽生产关系情调
  • 深入理解Linux网络 读书摘要
  • 过程方法如何重塑企业DNA
  • 2025 年 11 月实木双锁扣地板厂家推荐排行榜,双锁扣地板,实木地板,锁扣地板,实木双锁扣地板公司推荐,精选优质品牌与选购指南
  • ftp配置 linux
  • 豆包优化提示词
  • ftp连接linux服务器
  • ftp连接linux
  • 2025年长沙心理咨询机构排名TOP出炉,婚姻情感/焦虑/孩子厌学/青少年厌学/夫妻关系/线上/孩子网瘾/情绪不好心理咨询企业哪家好
  • 2025俄罗斯电商开店工具必备大盘点,错过血亏
  • 中山股权咨询公司到底靠不靠谱?2025专业推荐
  • 2025氨气吸收装置厂家甄选:各有专长的实力企业
  • 11.19与11.20笔记
  • CG100 D70F34xx/D70F35xx VW MQB Instrument Adapter: No Wire Cutting or Pin Lifting Required
  • 为什么你工作10年还是原地踏步?因为你没有掌握这5项核心能力
  • 2025新加坡留学机构哪个好?优质机构深度解析
  • 2025残卫报警器厂家推荐:残卫报警器哪家好综合测评
  • 2025年机油滤底座实力厂家权威推荐榜单:机油滤底座配件/机油滤底座清洗/机油滤底座垫源头厂家精选
  • B2B外贸独立站建站服务商有哪些?2025最新测评推荐
  • ftp服务器linux服务器
  • 2025年11月北京财税公司评测推荐:行业现状与选择要点全览
  • LLM书籍参阅: 谷歌提示工程
  • 深度剖析2025:江西cpe膜厂家哪家强?看完再下单