C++ 多类型对象的想法
C++ Multitype object idea
我的程序需要大量的ANSI<=>UNICODE对话,所以我想到了创建多类型对象的想法,它将比addination函数和很多new/delete更容易转换所有东西。伪代码:
class CWchar // char based
{
public:
public static implicit operator wchar_t*(CWchar cw)
{
// converting cw.data to wchar_t
// up to U+FFFF conversion needed
}
public static implicit operator char*(CWchar cw)
{
return cw.data;
}
CWchar& CWchar::operator=(const char* c)
{
data = *c;
return *this;
}
CWchar& CWchar::operator=(const wchar_t* c)
{
//conversion to char* ...
return *this;
}
// add some smart pointers, garbage collector, and leave delete
private:
char* data;
}
这真的值得编码还是我应该考虑另一种解决方案?也许已经完成了项目?或者也许我错了,这个想法很糟糕?谢谢
这听起来很像编解码器。 这允许您在 Char* 和多字节 wchar_t* 流之间进行转换。 这是标准库的一部分。 Stroustrup的《C++编程语言》第三版对此有一个很好的附录。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- C++ 多类型对象的想法
- 在单个结构下管理多类型对象