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.
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
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 articlesC235 - C and C based languages - I/O in C++ 
Formatting and outputting your own classes in C++ - (2015-10-30) 
Left shift operator on an output stream object - C++ - (2015-10-30) 
Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21) 
Reading (and writing) files in C++ - (2012-07-18) 
Printing objects in C++ - (2011-08-13) 
C++ - putting the language elements together into a program - (2011-01-08) 
Comparing Objects in C++ - (2008-06-13) 
Some new C programming examples - files, structs, unions etc - (2007-12-19)C238 - C and C based languages - Templates 
Using a vector within an object - C++ - (2013-01-19) 
Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06) 
Templates in C++ - defining a family pattern of methods / functions - (2011-08-12) 
Collections in C and C++ - arrays, vectors and heap memory blocks - (2011-04-12)C239 - C and C based languages - Putting it all together 
When do I use the this keyword in C++? - (2015-10-29) 
Test driven development, and class design, from first principles (using C++) - (2014-12-30) 
Learning to program - comments, documentation and test code - (2014-11-22) 
Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25) 
Using C and C++ functions in the same program - how to do it - (2010-11-24) 
Further C++ material - view new or old - (2010-07-04) 
Make and makefiles - a commented example to help you learn - (2010-03-12) 
Multiple Inheritance in C++ - a complete example - (2010-03-12) 
Compile but do not run PHP - syntax check only - (2010-02-22) 
Good Programming practise - where to initialise variables - (2007-05-09) 
Code quality counts - (2006-11-26) 
C++ - just beyond the basics. More you can do - (2006-11-14) 
Build on what you already have with OO - (2006-08-17)
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