Thread: overloaded <<

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    overloaded <<

    Code:
    // Object definition
    ...
    int     name;
    void* hook;  //the variable that holds the info
    ...
    std::istream &operator>>(std::istream &stream, Object &o)
    {
      char temp[100];
    
      stream >> name;
      stream.getline(temp, 100);
      o.hook = &temp;
      // the following gives the output I want 
      std::cout << reinterpret_cast<const char *>(o.hook);
      return stream;
    }
    
    std::ostream &operator<<(std::ostream &stream, const Object &o)
    {
      stream << name;
      stream << reinterpret_cast<const char *>(o.hook);
    
      return stream;
    }
    
    // Main program
    ...
    Object object;
    ...
    //the following gives garbage for "hook" only
    std::cout << object;
    What causes the problem above?
    Last edited by alphaoide; 03-15-2004 at 06:08 PM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Problem using overloaded << operator
    By Zildjian in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2004, 01:09 PM
  3. setw() type of overloaded <<
    By Thantos in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2004, 10:25 AM
  4. overloaded << AP classes
    By lithium in forum C++ Programming
    Replies: 12
    Last Post: 02-11-2003, 01:42 AM
  5. overloaded << operator doesnt work
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2002, 04:20 AM