gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:56:56 25 4
gpt4 key购买 nike

我可以重载 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com