Topic: fstream constructors and strings
Author: Kresimir Fresl <fresl@master.grad.hr>
Date: 1996/03/28 Raw View
Why are there no ifstream and ofstream constructors that take a
std::string rather than plain char* for the ``file name''?
The following `idiom' was, I think, often used (I use `S', and not `s',
because this is not a proposed standard string class):
cout >> "Enter file name -> ";
String file_name;
ifstream (file_name); // or: ofstream (file_name);
Declaring file_name as String, instead of char[file_name_length],
one does not have to worry about reserving space for file name
(file_name_lengths are very different on different operating systems).
The call to istream/ostream constructor is possible because String
classes use to have:
String::operator const char*() const
According to proposed standard one should write:
ifstream (file_name.c_str());
because implicit conversion of string to const char* is not defined.
I agree that implicit conversion is (almost always) dangerous, and I
_do not_ propose to add conversion operator to string class. Instead, I
think that fstream classes, beside (or even instead of) constructors that
take const char*s, should have constructors that take strings.
In many C++ books we can read that plain C arrays (including char[]s
and char*s) are to low level, and that we should use vector and string
classes. Also, it is supposed that iostream classes and operators are
`on the higher level of abstraction' than cstdio types and functions.
So, why do we have to manualy convert `high level constructs' to `low
level constructs'?
Kresimir Fresl
Faculty of Civil Engineering, Zagreb, Croatia
fresl@master.grad.hr
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: abell@mindspring.com (Andrew Bell)
Date: 1996/04/01 Raw View
Kresimir Fresl <fresl@master.grad.hr> wrote:
>According to proposed standard one should write:
> ifstream (file_name.c_str());
>because implicit conversion of string to const char* is not defined.
>I agree that implicit conversion is (almost always) dangerous, and I
>_do not_ propose to add conversion operator to string class.
Is there an explanation for why implicit conversion (at least to char
*) is dangerous? It doesn't seem to me that requiring the member
function call is that much safer, although admittedly it is easier to
grep for. However, the added complexity of code w/o a conversion
operator seems like it would add almost as many problems.
I checked out the std.c++ faq, and I've read the lang.c++ faq not too
long ago, and I didn't see a reference to this in either.
Andrew Bell
abell@mindspring.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]