Topic: Mandated char* in STD library
Author: zeil@amon.cs.odu.edu (Steven J. Zeil)
Date: 1995/10/18 Raw View
As I understand the I/O library description in the Working Paper, the
following code will NOT be legal:
...
// Get file to process
string fileName;
cout << "Enter filename: " << flush;
cin >> fileName;
ifstream input (fileName);
// Process file "input"
...
because the ifstream (and ofstream) constructors expect the filename
as a char* style string, rather than the string class from the
standard <string>.
To accomplish the above, it looks like I must change the input
declaration to
ifstream input(fileName.c_str());
So far, this is the only place I've seen where the use of char* rather
than string is mandated for programs that work entirely within the C++
world, not trying to link to C routines.
1) Are there others that I've missed?
[Of course, there are routines that return char*, but the
string(const char*) constructor supplies an "automatic"
conversion in such cases.]
2) Is this an oversight, or is there a rationale for avoiding "string"
in these cases?
This bothers me when I think of trying to teach C++. For the beginner,
the ability to use the string class seems like a significant
advantage. Having to teach both forms of character strings, and
lacking an intuitive rule for when one must abandon string and use
char* [e.g., by calling c_str()] is an annoying complication.
=================================
Steven Zeil
Old Dominion University
=================================
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]