Indispensable Windows Native-C + +: function overloading, default parameters, inline functions, function templates

Source: Internet
Author: User

[SOURCE DOWNLOAD]


Indispensable Windows Native-C + +: function overloading, default parameters, inline functions, function templates



Webabcd


Introduced
Essential C + + of Windows Native

    • function overloading
    • Default parameters
    • inline functions
    • function templates



Example
1, function overloading, default parameters
CppFunction1.h

#pragma<string>usingnamespace  std; namespace nativedll{    class  CppFunction1    {    public:         string Demo ();    };}

CppFunction1.cpp

/** function overloading, default parameters*/#include"pch.h"#include"CppFunction1.h"#include"CppHelper.h" using namespaceNativedll;stringCppFunction1::D emo () {//function Overloading (overload)-function overloading based on parameter type and number (cannot do function overloading by return value type only)    stringFunction1_get (); stringFunction1_get (stringS1); stringFunction1_get (inti1); stringRESULT1 = Function1_get ();//Function1_get    stringRESULT2 = Function1_get ("ABC");//function1_get ABC    stringRESULT3 = Function1_get ( -);//Function1_get//default parameter-the function parameter allows default values, and after providing a default value for a parameter, you must provide a default value for the parameters that follow it    intFunction1_sum (intXinty =1,intz =2); intA = Function1_sum ( -);//103    intb = Function1_sum ( -, -);//202    intc = Function1_sum ( -, -, -);// -    return "look at the code and the comments.";}//If a function parameter has a default value and a function declaration, the default value should be specified at the function declaration (at which point the default value of the function is not valid at the function definition)intFunction1_sum (intXintYintz) {returnX + y +Z;}//The following functions are used to demonstrate function overloadingstringFunction1_get () {return "Function1_get";}/*The function overload cannot be done only by the return value type (there is a compile error for this sentence) int function1_get () {return;}*/stringFunction1_get (stringS1) {    return "Function1_get"+S1;}stringFunction1_get (intI1) {    return "Function1_get"+int2string (I1);}


2. Inline functions, function templates
CppFunction2.h

#pragma<string>usingnamespace  std; namespace nativedll{    class  CppFunction2    {    public:         string Demo ();    };}

CppFunction2.cpp

/** Inline functions, function templates*/#include"pch.h"#include"CppFunction2.h" using namespaceNativedll;voidfunction2_demo1 ();voidFunction2_demo2 ();stringCppFunction2::D emo () {//inline (inline) functionsFunction2_demo1 (); //functions templates (function template)Function2_demo2 (); return "look at the code and the comments.";}//declares an inline function (left plus inline)InlineintFunction2_max (intIintj);//use of inline (inline) functionsvoidFunction2_demo1 () {intA =1, B =2; intx; //inline functions (built-in functions, inline functions, inline)-embed the code of the called function directly into the keynote function at compile timex = Function2_max (A, b);//2    /*The inline function is replaced directly at compile time (like a macro substitution), which calls the inline function and is replaced with the following code at compile time if (a > B) x = A;    x = b; */}//define an inline function (left plus inline)//Note: From the principle of inline, it is at the expense of code bloat (copy), eliminating the overhead of function calls, thus improving the efficiency of function execution.//1. When a function contains complex control statements, such as a loop statement or a switch statement or recursion, it is not advisable to use the inline function//2, generally only small size (5 sentences below) and the use of frequent functions declared as inline//3, in short, if not worth the (performance of small, code expansion Large), it is recommended not inlineInlineintFunction2_max (intIintj) {if(I >j)returni; returnJ;}//declares a function template with two indeterminate types, T1 and T2 (no difference between TypeName and Class)Template<typename T1,classT2>//define a function using the type defined in the function template aboveT1 Function2_template (T1 A, T2 B,intC//a function that uses a function template is a template function{    if(A >b)returnA; returnA + -;}/*this notation is incorrect because the type of the return value cannot be deduced T2 function2_template (T1 A, T1 b) {if (a > B) return to [+];}*/voidFunction2_demo2 () {floatresult; floati =1.1f; intj =2; //invokes the specified function, which has an indeterminate type of argumentresult = Function2_template (i, J,0);//101.1}



Ok
[SOURCE DOWNLOAD]

Indispensable Windows Native-C + +: function overloading, default parameters, inline functions, function templates

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.