Newsgroups: comp.lang.c++ Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!caen!caen.engin.umich.edu!brucew From: brucew@caen.engin.umich.edu (Bruce Hugh Wilson) Subject: Reading in objects (of different classes) from a text file Message-ID: <1991Mar28.195840.19270@engin.umich.edu> Keywords: cin, file i/o Sender: news@engin.umich.edu (CAEN Netnews) Reply-To: brucew@caen.engin.umich.edu (Bruce Hugh Wilson) Organization: University of Michigan Engineering, Ann Arbor Date: Thu, 28 Mar 1991 19:58:40 GMT I'm writing an application which involves inputting/saving/reading (from a keyboard and text file) objects of different classes, but with a common parent class. It's easy enough to write various overloaded << operators to handle objects of different classes- but this is because the operators can recognize the class the object belongs to. A sample file output might look like: motor1, 3.4, 5.4, .001 ... load1, 4.3 big_shaft,10.8,0.1 ... Is there a way to avoid the use of a switch statement in reading this info in? My current strategy entails reading and using a switch statement, creating an object of the appropriate class, and then reading in the next line of the file. Is there a simpler/better way?