357a C++ - unknown array size, unknown object type. Help!
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
C++ - unknown array size, unknown object type. Help!

Question MarkThere 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 under
C239 - C and C based languages - Putting it all together
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
  [3067] Using C and C++ functions in the same program - how to do it - (2010-11-24)
  [2851] Further C++ material - view new or old - (2010-07-04)
  [2674] Make and makefiles - a commented example to help you learn - (2010-03-12)
  [2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
  [2646] Compile but do not run PHP - syntax check only - (2010-02-22)
  [1181] Good Programming practise - where to initialise variables - (2007-05-09)
  [945] Code quality counts - (2006-11-26)
  [925] C++ - just beyond the basics. More you can do - (2006-11-14)
  [836] Build on what you already have with OO - (2006-08-17)

C235 - C and C based languages - I/O in C++
  [3807] Reading (and writing) files in C++ - (2012-07-18)
  [3390] Printing objects in C++ - (2011-08-13)
  [3124] C++ - putting the language elements together into a program - (2011-01-08)
  [1675] Comparing Objects in C++ - (2008-06-13)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)

C238 - C and C based languages - Templates
  [3982] Using a vector within an object - C++ - (2013-01-19)
  [3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
  [3388] Templates in C++ - defining a family pattern of methods / functions - (2011-08-12)
  [3245] Collections in C and C++ - arrays, vectors and heap memory blocks - (2011-04-12)


57c6
Back to
C++ - objects that are based on other objects, saving coding and adding robustness
Previous and next
or
Horse's mouth home
Forward to
Is this number between? Does this list include? - Ruby
Some other Articles
Displaying a directory or file system tree - Linux
Process every member of an array, and sort an array - Ruby
Multiple inputs, multiple out, ruby functions
Is this number between? Does this list include? - Ruby
C++ - unknown array size, unknown object type. Help!
C++ - objects that are based on other objects, saving coding and adding robustness
C++ - how we teach the language and the concepts behind the language
Events - Spring and Early Summer 2011 in Melksham
What have these pictures in common?
Light and dark at Green Park
4086 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2013: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3252_C-u ... Help-.html • PAGE BUILT: Sat Feb 23 12:39:13 2013 • BUILD SYSTEM: wizard
0