11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

358Part V: Optional FeaturesNotice that the TemplateVector handles both int values and Name objectswith equal ease. Notice also how similar the nameFn() and intFn() functionsare, even though integers and names have nothing to do with each other.A sample session appears as follows:Give me a series integer values to add to a vector(Enter a negative number to terminate):51015-1Here are the numbers you entered0:51:102:15Enter names(Enter an ‘x’ to quit):ChesterFoxPennyxHere are the names you entered0:Chester1:Fox2:PennyPress any key to continue . . .Do I Really Need Template Classes? “But,” you say, “can’t I just create a simple Array class? Why mess withtemplates?”Sure you can, if you know a priori what types of things you need arrays for.For example, if all you ever need is arrays of integers, you have no reason tocreate a template Vector; you could just create the class IntArray andbe finished.The only other alternative is to use void*, which can point to any type ofobject. The following VoidVector program is based upon the use of voidpointers:// VoidVector - implement a vector that relies on void*// as the storage element#include #include #include

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!