如何定义从std::string到自定义类型的类型转换
How to define a type conversion from std::string to a custom type?
转换运算符在类中声明,如下所示:
operator string();
当声明时,它们可以这样定义;
Foo::operator string(){ return stringRep; }
如何定义从字符串到自定义类型的转换运算符
编写一个接受字符串的构造函数。
operator Tc
是一个非静态成员函数,它将Ty类型转换为某个指定类型。
如果您需要从字符串(类型为std::basic_string<char>
的类)转换为类型为Tc
的字符串,请使用
std::basic_string
,需要提供类型转换功能,或者- 您需要在Tc类型的类中提供一个构造函数,该构造函数接受
std::basic_string<char>
类型的对象
相关文章:
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++自定义比较函数
- 如何比较自定义类的std::变体
- std::设置自定义比较器
- 自定义堆栈上 std::string 数据的输出
- 带有自定义分配器的std::string
- C++ 标准::设置<string>字母数字自定义比较器
- C++自定义模板LinkedList在添加std::string时崩溃
- `std::string`分配是我当前的瓶颈-如何使用自定义分配器进行优化
- 创建自定义" string "(结构)副本
- 使用自定义排序功能对向量<string>中的字符串进行排序时出现分段错误
- 用户自定义string::reserve()
- 在std::string中使用自定义分配器来重用已分配的字符缓冲区
- 引用std::basic_string特化string与自定义分配器作为std::string的常量对象,没有开销
- 序列化包含std::string的自定义Struct
- 带有自定义类的范围循环(矢量的简单版本<string>,使用分配器)
- 如何定义从std::string到自定义类型的类型转换
- 将std::string和自定义对象存储在std::pair中
- 如何设置std::vector<std::string>每个元素中的自定义字符串