Software Information |
C++ Function Templates
C++ Function templates are those functions which can handle different data types without separate code for each of them. For a similar operation on several kinds of data types, a programmer need not write different versions by overloading a function. It is enough if he writes a C++ template based function. This will take care of all the data types. There are two types of templates in C++, viz., function templates and class templates. This article deals with only the function templates. There are lot of occasions, where we might need to write the same functions for different data types. A favorite example can be addition of two variables. The variable can be integer, float or double. The requirement will be to return the corresponding return type based on the input type. If we start writing one function for each of the data type, then we will end up with 4 to 5 different functions, which can be a night mare for maintenance. C++ templates come to our rescue in such situations. When we use C++ function templates, only one function signature needs to be created. The C++ compiler will automatically generate the required functions for handling the individual data types. This is how a programmer's life is made a lot easier. C++ Template functions - Details: Let us assume a small example for Add function. If the requirement is to use this Add function for both integer and float, then two functions are to be created for each of the data type (overloading). int Add(int a,int b) { return a+b;} // function Without C++ template float Add(float a, float b) { return a+b;} // function Without C++ template If there are some more data types to be handled, more functions should be added. But if we use a c++ function template, the whole process is reduced to a single c++ function template. The following will be the code fragment for Add function. template T Add(T a, T b) //C++ function template sample { return a+b; } This c++ function template definition will be enough. Now when the integer version of the function, the compiler generates an Add function compatible for integer data type and if float is called it generates float type and so on. Here T is the typename. This is dynamically determined by the compiler according to the parameter passed. The keyword class means, the parameter can be of any type. It can even be a class. C++ Template functions - Applicability: C++ function templates can be used wherever the same functionality has to be performed with a number of data types. Though very useful, lots of care should be taken to test the C++ template functions during development. A well written c++ template will go a long way in saving time for programmers. About The Author Muthukumar More articles can be found at http://www.codersource.net/.
MORE RESOURCES: Unable to open RSS Feed $XMLfilename with error HTTP ERROR: 404, exiting |
RELATED ARTICLES
International Support - Microsoft Great Plains VAR/Partner Selection - Overview for V.P. IT Looks like Microsoft Great Plains becomes more and more popular, partly because of Microsoft muscles behind it. It seems to be goof solution for mid-size and even large multinational corporation with Headquarters in US and branches Worldwide. Putting Screensavers Under Control No matter how much you enjoy your favorite screensavers, sometimes they can be rather annoying. Don't like them interrupting your presentations? Hate them disturbing you watching movies? Look no further. RFID: Strengthen the Position for SAP; United States SAP Inc., a global leader in client/server enterprise application software that for years been the backbone to RFID application has seen a 1,000% spike year-over-year in customers, interested in RFID deployment. Programming Environments And The Software Production Process Introduction:The creating of a computer program involves a number of stages which can be made easier with several separate software utilities. There are now integrated set of support programs which combine all the necessary utilities to provide a complete program development environment. CRM and Customer Life Cycle Customer Relationship Management or CRM is a combination of enterprise strategies, business processes and information technologies used to learn more about customers' needs and behaviors in order to develop stronger relationships with them. CRM software systems automate many customer-related business tasks. Crystal Reports For Microsoft Navision - Overview For Programmer/IT Specialist Let us give you - developer some hints in the report creation.C/SIDE database - use C/ODBC to create the connection for your Crystal Report. Microsoft Great Plains: Offshore Customization & Development - Overview for Consultant When you visit department stores and see that majority of the apparels are produced in other countries, not US - you probably do not worry about this fact. About 10 years ago, when Clinton's high tech era was at the inception - we saw the signs of high tech professionals inflow in the USA from overseas. SQL: Querying Microsoft Great Plains - Overview for Database Administrator/Developer Looks like Microsoft Great Plains becomes more and more popular, partly because of Microsoft muscles behind it. Now it is targeted to the whole spectrum of horizontal and vertical market clientele. Corel WordPerfect 7 Macro Programming Example Case study:A secretary using Corel WordPerfect 7 is often required to carry out calculations and insert the values into invoices or other documents.She does this by entering calculations into either a real calculator and transcribing the answer, or the Microsoft calculator and copying then pasting the result into the document. Understanding XML Server XML Server can be a Web Server that stores the XML files in it and serves them on demand. The XML Server would have processing capabilities with an XML engine and to transform the XML document to other forms. Great Plains Custom Development: Dexterity, VBA, SQL, Crystal, eConnect - Overview For Programmer Microsoft Great Plains is main Microsoft Business Solutions accounting package for the US market. It has multiple customization/modification/reporting tools. Microsoft Great Plains: exchange & brokerage - implementation notes If you company is small or mid-size special products or materials exchange broker, you probably have custom in-house made exchange application. Nowadays exchange is done over the internet, so you might have advanced web-based exchange application. ERP Consulting: Microsoft Great Plains Partner Future Directions In the Clinton era the status quo was simple: you serve your local clients and if you want to expand your business - open offices in other business metros. Great Plains Software was granting generous margins on the software sales and stimulated local leading VARs/Partners/Resellers with so-called orphan leads - customers who lapsed in payment annual enhancement program or called Great Plains Software directly to comply about quality of service by their VAR. Microsoft Navision Customization and Reporting - Tips For Programmer/IT Specialist C/SIDE (Client/Server Integrated Development Environment) - The core of Navision is the C/SIDE. C/SIDE is the foundation for all the business management functionality of Navision. Microsoft Great Plains GL: General Ledger - Overview For Consultant Microsoft Business Solutions Great Plains is marketed for mid-size companies as well as Navision (which has very good positions in Europe and emerging markets where it can be easily localized). Great Plains Inventory control is pretty robust and here we would like to give you highlights on standard functionality as well as what could be added to its standard features. Microsoft Great Plains on Ctree or Pervasive SQL - What to Do - Tips for IT Manager As you probably know, when Microsoft purchased Great Plains Software - the whole strategy for Great Plains Dynamics/eEnterprise line was changed. Initial GPS strategy was to maintain DB platform independence - via it's C-written engine Dexterity, based on the believe that C programming language is platform independent. Separate Anti-Keylogging Protection: Who Needs it Most? If there still are few unprotected computers left, I haven't seen any.Every emerging threat causes means of defense to appear. DVD Burning Tips CD and DVD replication is a process that works by creating discs from scratch. Not to be confused with duplication (the act of burning information onto readymade, commercially-available blank CD-Rs and DVD-Rs), replication relies on the use of molds to produce actual discs that already contain digital data. Microsoft CRM in Latin America: Implementation, Customization, Support - Overview for Consultant Microsoft Business Solutions CRM is present several years on the US software market plus it makes advances in Europe. We expect substantial number growth of Microsoft CRM implementations across South and Central America and the need for Microsoft CRM implementation, support, tuning, reporting, training services, especially in such countries as Mexico, Columbia, Venezuela, Argentina, Peru, Uruguay, Chili, Costa Rica. Cisco Certification: Introduction To ISDN, Part III Configuring PPP PAP AuthenticationNow we know how the ISDN link comes up (interesting traffic), and some scenarios that might cause the link to stay up, we need to look at ISDN authentication schemes. The two methods Cisco certification candidates must be familiar with are PAP and CHAP. |
home | site map | contact us |