C++ - unknown array size, unknown object type. Help!

There are time when you're writing a program where you say "I don't know".
• I don't know how many records / object I'm going to read from a data file into an array - so I can't set the size of my array
• I don't know what sort of object I'll be creating from a line I've read from a data file until I analyse it, so I can't call the constructor directly.
You
could write a program to read take up to a certain maximum number of records / objects, but one day you'll find that someone want to run that program with more records than you've allowed for. The
solution (C++ answer / term) is to use a vector - which is line an array or objects in many ways, but can dynamically expand. It saves you writing you own code with
realloc calls.
And you
could write a program which analyses incoming data lines before calling the appropriate constructor - but by doing so you're moving code which really should be in the class into each application. The
solution is to provide a static method in the base class of all possible objects, which analyses the data and decides which constructor to call. Such a method is known as a factory method.
An example showing these techniques - from the C++ course I ran the week before last - is
[here]. It also shows some file and string handling - a little bit of everything as I pulled together elements of the C++ language into a very straightforward application, but one covering many aspects.
I might not know how big my array should be, or what type of data each line will contain - but I do know that our next series of C and C++ courses starts at the very end of May - see
[here]. If you've missed those, there are further courses in August, and in the autumn - use the same link as we'll update our page.
(written 2011-04-17)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C239 - C and C based languages - Putting it all together [836] Build on what you already have with OO - (2006-08-17)
[925] C++ - just beyond the basics. More you can do - (2006-11-14)
[945] Code quality counts - (2006-11-26)
[1181] Good Programming practise - where to initialise variables - (2007-05-09)
[2646] Compile but do not run PHP - syntax check only - (2010-02-22)
[2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
[2674] Make and makefiles - a commented example to help you learn - (2010-03-12)
[2851] Further C++ material - view new or old - (2010-07-04)
[3067] Using C and C++ functions in the same program - how to do it - (2010-11-24)
[3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
[3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
[4326] Learning to program - comments, documentation and test code - (2014-11-22)
[4374] Test driven development, and class design, from first principles (using C++) - (2014-12-30)
[4559] When do I use the this keyword in C++? - (2015-10-29)
C238 - C and C based languages - Templates [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
[3245] Collections in C and C++ - arrays, vectors and heap memory blocks - (2011-04-12)
[3388] Templates in C++ - defining a family pattern of methods / functions - (2011-08-12)
[3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
[3982] Using a vector within an object - C++ - (2013-01-19)
C235 - C and C based languages - I/O in C++ [1675] Comparing Objects in C++ - (2008-06-13)
[3124] C++ - putting the language elements together into a program - (2011-01-08)
[3390] Printing objects in C++ - (2011-08-13)
[3807] Reading (and writing) files in C++ - (2012-07-18)
[4562] Left shift operator on an output stream object - C++ - (2015-10-30)
[4563] Formatting and outputting your own classes in C++ - (2015-10-30)
Some other Articles
Displaying a directory or file system tree - LinuxProcess every member of an array, and sort an array - RubyMultiple inputs, multiple out, ruby functionsIs this number between? Does this list include? - RubyC++ - unknown array size, unknown object type. Help!C++ - objects that are based on other objects, saving coding and adding robustnessC++ - how we teach the language and the concepts behind the languageEvents - Spring and Early Summer 2011 in MelkshamWhat have these pictures in common?Light and dark at Green Park