Thread: getline() vs cin.getline()

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    getline() vs cin.getline()

    Why are there two getlines, one for char* and one for std::string? Why is getline a seperate function for std::strings and a part of an istream for char*?

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    String isn't guaranteed to be a type. It is only defined if the string header was included. Hence, if the istream header was included (and the string header wasn't), there would be an undefined error. However, if there was an #ifdef, it wouldn't work if string was included after istream. The only solution would be to #define to string symbol before the headers. I guess the standards committee feel it is more elegant to use a global function instead (and eliminate the possible #define conflict).

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Further, istream is old. It was originally a class (now it's a typedef for a templated class), and istream existed (with virtually the same interface) since the oldest days of C++. Long before there was a std::string (or even a std namespace, or even namespaces at all).

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Yeah, but why would they be afraid to update a header file?

  5. #5
    I think this goes back to If its not broken, don't fix it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM