相关疑难解决方法(0)

为什么(i | o)fstream对文件名采用const char*参数?

为什么openstd ::(i | o)fstream类的构造函数和方法将文件的名称作为参数以a的形式const char*而不是std::string?似乎STL的创建者想要使用他们所写的内容而不是使用他们编写的类来替换它.

c++ history stl

16
推荐指数
3
解决办法
1050
查看次数

std :: ifstream类的设计

我们这些看过STL之美的人试图尽可能地使用它,并且还鼓励其他人在使用原始指针数组的任何地方使用它.斯科特迈耶斯写了一本关于STL的全书,题目为有效STL.然而,发生了什么事的开发商ifstream,他们首选char*std::string.我想知道为什么第一个参数ifstream::open()是类型const char*,而不是const std::string &.请看一下它的签名:

void open(const char * filename, ios_base::openmode mode = ios_base::in );
Run Code Online (Sandbox Code Playgroud)

为什么这个?为什么不这样:

void open(const string & filename, ios_base::openmode mode = ios_base::in );
Run Code Online (Sandbox Code Playgroud)

这是设计的一个严重错误吗?或者这个设计是故意的?可能是什么原因?我不明白为什么他们宁愿任何理由char*std::string.注意我们仍然可以传递char*给后面的函数std::string.那不是问题!

顺便说一句,我知道这ifstream是一个typedef,所以没有评论我的头衔.:P.它看起来很短,这就是我使用它的原因.

实际的类模板是:

template<class _Elem,class _Traits> class basic_ifstream;
Run Code Online (Sandbox Code Playgroud)

c++ stl class-design std standard-library

5
推荐指数
2
解决办法
366
查看次数

标签 统计

c++ ×2

stl ×2

class-design ×1

history ×1

standard-library ×1

std ×1