Thread: my string class

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    my string class

    I am trying to write my own string class but I have trouble writing:
    operator<<
    operator>>
    How do I access ostream and istream?
    Please help?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Declare them as friend functions of your class. The function prototypes might be like this:
    Code:
    std::ostream& operator<<(std::ostream&, const T&);
    std::istream& operator>>(std::istream&, T&);
    for some class T for which these are friend functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    thanx laserlight but it didnt really work, maybe you can explain it to me a little more

  4. #4
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    The custom inserter and extractor can be as simple as this:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    
    class String {
      char *mem;
      int size;
    public:
      String ( const char *p );
      ~String();
    
      friend std::istream& operator>> ( std::istream& is, String& s );
      friend std::ostream& operator<< ( std::ostream& os, const String& s );
    };
    
    String::String ( const char *p )
    {
      size = strlen ( p );
      mem = new char[size + 1];
      strcpy ( mem, p );
    }
    
    String::~String() { delete [] mem; }
    
    std::istream& operator>> ( std::istream& is, String& s )
    {
      return is.getline ( s.mem, s.size );
    }
    
    std::ostream& operator<< ( std::ostream& os, const String& s )
    {
      return os << s.mem;
    }
    
    int main()
    {
      String s = "My cool string";
    
      std::cout << s << "\nEnter a string: ";
      std::cin >> s;
      std::cout << s << '\n';
    }
    Last edited by Slacker; 12-26-2005 at 01:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM