工艺——使用类进行指针转换
来源:互联网 发布:用什么软件转码快 编辑:程序博客网 时间:2024/05/17 07:24
场景:当使用外部函数时,会遇到参数类型和内部使用的类型不同,如内部使用int,参数使用short。特别是作为输出参数的指针类型。如:
方法:编写一个转换用的类,可使代码简单。
进一步:该方式写成模板适用性会更广。
void ExternalFunc(short* value);
void InternalFunc(int* value) {
...
short valueIn = *value;
ExternalFunc(&valueIn);
*value = valueIn;
...
}
需要增加转换代码,显得比较罗嗦。void InternalFunc(int* value) {
...
short valueIn = *value;
ExternalFunc(&valueIn);
*value = valueIn;
...
}
方法:编写一个转换用的类,可使代码简单。
class IntPtrToShortPtr {
public:
IntPtrToShortPtr(int* intValue) : m_IntValue(intValue) { m_ShortValue = *m_IntValue; }
~IntPtrToShortPtr() { *m_IntValue = m_ShortValue; }
operator short*() { return &m_ShortValue; }
protected:
int* m_IntValue;
short m_ShortValue;
};
void InteralFunc(int* value) {
ExternalFunc(IntPtrToShortPtr(value));
}
这个方式的优点是语义连贯,没有干扰阅读的代码。public:
IntPtrToShortPtr(int* intValue) : m_IntValue(intValue) { m_ShortValue = *m_IntValue; }
~IntPtrToShortPtr() { *m_IntValue = m_ShortValue; }
operator short*() { return &m_ShortValue; }
protected:
int* m_IntValue;
short m_ShortValue;
};
void InteralFunc(int* value) {
ExternalFunc(IntPtrToShortPtr(value));
}
进一步:该方式写成模板适用性会更广。
0 0
- 工艺——使用类进行指针转换
- 焊接工艺技巧与电烙铁使用——经验篇
- 使用指针进行交换
- 《软件工艺》——中译本序
- 自动化基础课程——工艺
- 【烙铁使用规范】—— 延长无铅手工焊接工艺中烙铁头的寿命
- 利用指针进行类型强制转换(c指针详解)
- $ynop$y$工艺库1——基本概念
- $ynop$y$工艺库2——逻辑库
- 自动控制的故事——工艺与仪表专业
- 自动控制的故事——工艺与仪表专业
- 芯片相关工艺01——芯片反向工程
- 电子工艺实践—PCB设计的学习
- 使用PCF8591进行ADDA转换
- 使用shell进行大小写转换
- C# 使用Method进行转换
- 使用模板进行类型转换
- 使用Boost进行数据转换
- view动画
- 目前公司产品架构
- Nginx源码学习资源集
- windows进程间通信常用方法
- adblockplus for chrome代码编译
- 工艺——使用类进行指针转换
- C++文件在安卓上的转移(以NDK为工具)
- 回溯-八皇后问题
- nyoj-苹果
- 我的第一个CSDN博客
- 黑马程序员--oc结构体
- [BestCoder] Round #21 1002 - Formula | HDU 5139
- D - Vanya and Computer Game
- 常用降维方法