Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the string version of getline a non-member function?

Tags:

c++

string

std

The getline function has a character version that is a member function, as well as a global version that takes strings. Why aren't they both member functions? The current way makes it seems as though there isn't a string version.

like image 287
Casebash Avatar asked Dec 13 '10 22:12

Casebash


1 Answers

istream& istream::getline(char* s, streamsize n) is part of the stream interface.

istream& getline(istream& is, string& str) is an extension method from the string library (just like the istream &operator>>(istream&, string&)).

This design was probably chosen in order to decouple iostreams from string, as fstream::open() also does not take std::string arguments but rather const char*.

like image 135
Marcus Borkenhagen Avatar answered Sep 18 '22 07:09

Marcus Borkenhagen