🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Kresimir Fresl Fri, 22 Mar 1996 18:17
fstream constructor and strings
#3780
Author: Kresimir Fresl
Date: Fri, 22 Mar 1996 18:17
44 lines
2353 bytes
I think that the following `idiom' was often used:
    
    // necessary includes
    
    cout >> "Enter file name -> ";
    String file_name;
    ifstream (file_name); // or: ofstream (file_name);
    
(I used `S' (not `s') because this is not a proposed standard string 
class.) 

ifstream constructor expects const char*, but above call was possible 
because String classes (eg. in libg++) used to have:
    String::operator const char*() const
(or something similar). 

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 also have constructors that take strings.
In many C++ books one can read that plain C arrays (including char[]s and
char*s) are to low level, and that with classes (eg. vectors and strings) 
one can work on higher levels of abstraction. fstreams are `better' than
cstdio functions, but, because of const char* parameters, they are still
to low level. 


fresl

-----------
Kresimir Fresl
Faculty of Civil Engineering, Zagreb, Croatia
email: fresl@master.grad.hr
---
[ 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 
]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads