设计std :: ifstream类 [英] Design of std::ifstream class

查看:112
本文介绍了设计std :: ifstream类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经看到了STL的美丽尽可能使用它,并鼓励他人使用它,无论我们在哪里,使用原始指针数组

Those of us who have seen the beauty of STL try to use it as much as possible, and also encourage others to use it wherever we see them using raw pointers and arrays. Scott Meyers have written a whole book on STL, with title Effective STL. Yet what happened to the developers of ifstream that they preferred char* over std::string. I wonder why the first parameter of ifstream::open() is of type const char*, instead of const std::string &. Please have a look at it's signature:

void open(const char * filename, ios_base::openmode mode = ios_base::in );

为什么?为什么不这样:

Why this? Why not this:

void open(const string & filename, ios_base::openmode mode = ios_base::in );

这是设计的严重错误吗?还是这个设计是故意的?可能是什么原因?我没有看到任何理由为什么他们优先 char * 超过 std :: string 。注意我们仍然可以通过 std :: string 的后一个函数传递 char * 。这不是问题!

Is this a serious mistake with the design? Or this design is deliberate? What could be the reason? I don't see any reason why they have preferred char* over std::string. Note we could still pass char* to the latter function that takes std::string. That's not a problem!

顺便说一句,我知道 ifstream 是一个typedef,我的标题:P。

By the way, I'm aware that ifstream is a typedef, so no comment on my title.:P. It looks short that is why I used it.

实际的类模板是:

template<class _Elem,class _Traits> class basic_ifstream;


推荐答案

我的标准副本与您不同意。它说这两个都是重载:

My copy of the standard disagrees with you. It says both these are overloads:

void open(const char* s, ios_base::openmode mode = ios_base::in);
void open(const string& s, ios_base::openmode mode = ios_base::in);

然而,该标准的副本是下一版本标准的草稿,C ++ 0x。

However, that copy of the standard is a draft of the next version of the standard, C++0x.

这样做的原因是 iostreams 库早于 std :: basic_string ,并且因为图书馆设计师不想让某人拥有 #include< string> t想使用它。

The reason for this is that the iostreams library predates std::basic_string, and because the library designers didn't want someone to have to #include <string> and all it's other associated baggage if they didn't want to use it.

这篇关于设计std :: ifstream类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆