View Single Post
Old Feb 17th, 2008, 11:54 AM   #6
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Re: one class to implement a linked list of objects of another class

The & refers to a reference, which is a way of passing parameters to things such as functions.
I won't bother explaining references fully, as Narue does that nicely here.

By writing a function that takes a std::ifstream object, you can specify the source of the input.

For example:
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. void readNLines( int n, std::ifstream &in ) {
  6. std::string line;
  7.  
  8. for ( int x = 0; x < n; x++ ) {
  9. std::getline( in, line );
  10.  
  11. std::cout << x + 1 << ": " << line << std::endl;
  12. }
  13. }
  14.  
  15. int main() {
  16. // Make sure in real code, you check for success or failure
  17. // when opening files.
  18.  
  19. // Open this file for reading
  20. std::ifstream input( "example.cpp" );
  21. readNLines( 3, input );
  22. }
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote