c++ - 重载 std::string 构造函数

标签 c++ constructor-overloading

我可以重载 std::string 构造函数吗?

我想创建一个构造函数,它接受 std::wstring 并返回一个 std::string。这可能吗?如何实现?

谢谢。

最佳答案

Can I overload the std::string constructor?

不,它需要更改 std::string 声明。

I want to create a constructor which takes std::wstring and return a std::string. is it possible and how?

您可以改用转换函数,例如:

std::string to_string(std::wstring const& src);

但是,您需要决定如何处理无法使用 std::string 的 8 位编码表示的符号:是将它们转换为多字节符号还是抛出一个异常(exception)。参见 wcsrtombs功能。

关于c++ - 重载 std::string 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9262091/

相关文章:

c++ - 在纹理中的 CPU 和 GPU 之间传递数据

c++ - 逐个删除动态数组的元素

c++ - 根据不同函数排序的 std::vectors 的交集

c++ - 预期不合格 - ID _config

c++ - 编译时字符串 : constructor overload precedence between `const char *` / `const char[]`

c++ - 模板类构造函数重载决议

c++ - C++/OpenSSL将PEM加载到STACK_OF(X509)

c++ - 如何使用动态内存分配将值传递给对象构造函数的声明

c++ - 为什么 const char* 隐式转换为 bool 而不是 std::string?