getline() vs cin.getline()

This is a discussion on getline() vs cin.getline() within the C++ Programming forums, part of the General Programming Boards category; Why are there two getlines, one for char* and one for std::string? Why is getline a seperate function for std::strings ...

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

    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,136
    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
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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,136
    Yeah, but why would they be afraid to update a header file?

  5. #5
    Perverted unanimous's Avatar
    Join Date
    Oct 2001
    Posts
    336
    I think this goes back to If its not broken, don't fix it.
    Give me a bad reputation!!!

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, 11: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, 12:12 PM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21