Q&A

Q1:Why use templates when macros will do?
A1: Templates are type-safe and built into the language.
Q2:What is the difference between the parameterized type of a template function and the parameters to a normal function?
A2: A regular function (non-template) takes parameters on which it may take action. A template function allows you to parameterize the type of a particular parameter to the function.
Q3:When do you use templates and when do you use inheritance?
A3: Use templates when all the behavior or virtually all the behavior is unchanged, but the type of the item on which your class acts is different. If you find yourself copying a class and changing only the type of one or more of its members, it might be time to consider using a template. ...

Get Sams Teach Yourself C++ in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.