Re: more questions...
On Apr 19, 9:31 am, Obnoxious User <O...@127.0.0.1> wrote:
On Sun, 19 Apr 2009 08:10:51 +0200, Mark S. wrote:
Obnoxious User wrote:
'char *'-strings are old school C strings. Avoid them if
you're learning. Use only std::string. It's a complete
design failure that the fstream classes don't accept
std::string.
Just to make sure: You mean that I should use c_str()
whenever possible? I am still confused when "some text" is
treated as a string and when as a char*. But I guess I'll
figure it out eventually as time goes on.
Any text string in double quotes is of type 'char const *'.
"text"
String literals have type char const[] in C++, not type char
const*. They will, of course, convert to char const* in most
contexts, they will also convert to std::string in many
contexts, and to char* in a few special contexts.
As for general rules as to when to use what, I'd argue that
anytime you have to declare a variable, it should be
std::string, and you'll use c_str() if you need to use a legacy
interface. On the other hand, I don't have any problem with:
std::ifstream in( "constantFileName" ) ;
rather than
std::ifstream in( std::string( "constantFileName" ).c_str() ) ;
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34