c++ - 为什么 std::ifstream 构造函数不采用 std::string?

标签 c++ string constructor std ifstream

<分区>

Possible Duplicate:
Design of std::ifstream class
Why does (i|o)fstream take a const char* parameter for a file name?

查看 std::ifstream 的构造函数,我发现了两个:

ifstream ( );
explicit ifstream ( const char * filename, ios_base::openmode mode = ios_base::in );

为什么第二个采用 const char * 而不是 const std::string &

这是某种避免循环或前向引用的方法吗?

最佳答案

这是一个简单的遗漏。没有人及时想到它。这已在 C++11 中得到纠正,其中 std::string 也被接受。来自 27.9.1.7/3:

explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);

Effects: the same as basic_ifstream(s.c_str(), mode).

关于c++ - 为什么 std::ifstream 构造函数不采用 std::string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12574594/

相关文章:

java - 构造函数可以在Java中私有(private)吗

c++ - operator= 在我的自定义字符串类中不能正常工作

c++ - 返回值语法重载 const 和非 const 函数

c++ - 在工具栏上过度绘制?替代想法?

c++ - 使用 WinApi 时是否需要 wchar_t 支持 unicode?

ruby - 在不使用 BigDecimal 的情况下将美分转换为 Ruby 中的美元字符串

c++ - 视觉 C++ : No default constructor

python - 如何将其中包含换行符的字符串转换为 Python 中的列表?

C++在一行中组合字符串作为参数

c# - 如果构造函数不应该做任何工作,那么如何在 MVVM 中初始化 Views 和 ViewModels