Overloaded >>

This is a discussion on Overloaded >> within the C++ Programming forums, part of the General Programming Boards category; I need to use the overloaded >> this way Code: // while "std::cin >> process" is succesfull while ( std::cin ...

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

    Overloaded >>

    I need to use the overloaded >> this way
    Code:
    // while "std::cin >> process" is succesfull
    while (std::cin >> process && !(ph.isFull())) {
        // do something
    }
    Code:
    std::istream &operator>>(std::istream &stream, PCB &pcb)
      char* temp = new char[MAX_HOOK_LEN];     // Temporary holder for process info
    
      stream >> pcb.pid;
      if (pcb.pid == -1) {   // '-1' indicates end of input
        return ????   //what to return
      }
      stream >> pcb.arrival_time;
      stream >> pcb.last_cpu_burst;
      stream >> pcb.test_cpu_burst;
      stream >> pcb.priority;
      stream >> pcb.state_status;
      if (stream.getline(temp, MAX_HOOK_LEN, '\n')){
        pcb.hook = temp;
      }
     
      return stream;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    What you return is a reference to an istream (naturally), before you return you set the failbit to indicate that you could not read a new record, and eofbit to indicate that you cannot read another record because you have read them all.

    Code:
    std::istream &operator>>(std::istream &stream, PCB &pcb)
        PCB temp;
    
        stream >> temp.pid;
        if(temp.pid == -1) {
            stream.setstate(std::ios_base::failbit | std::ios_base::eofbit);
            return stream;
        }
        stream >> temp.arrival_time;
        stream >> temp.last_cpu_burst;
        stream >> temp.test_cpu_burst;
        stream >> temp.priority;
        stream >> temp.state_status;
        getline(stream,temp.hook);
        if(stream) pcb.swap(temp);
        return stream;
    }
    This assumes that PCB uses a std::string for it's hook, otherwise you have a memory leak in your previous code. This also obeys the convention that I always use that all >> operations leave thier targets unchanged. swap() is a new member that makes this and many other things much easier.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    hhmmm, interesting makeover you did there
    thanks for the new knowledge and insight
    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. Friend func. and overloaded >> and <<
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2005, 01:14 AM
  3. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 11:53 PM
  4. overloaded >> operator problem
    By quizkiwi in forum C++ Programming
    Replies: 7
    Last Post: 07-19-2005, 04:27 PM
  5. overloaded on overloads
    By emceedee in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2003, 02:14 AM

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