The basic concepts of C ++ (inline, templates, function)

1、inline function

      Embedded function calling function is called built-in functions (inline function), also known as inline functions, or inline functions. Specifies the built-in functions, just add a keyword inline function can be left in the first row. You can be simultaneously written in the function declaration inline and defined functions, which may be only one inline declarations, the same effect. Built-in functions can not include complex control statements, such as loop and switch statements, generally less than 5. To function as inline declaration, just a suggestion raised by compiling system, build system will decide whether to do so depending on the circumstances. To sum up, only those smaller but frequently called a simple function, it is suitable for declared as inline functions.

2、function overloading 

       C ++ allows multiple functions define the same function name, the number of different parameters and parameter types of these functions. In addition allows overloaded functions of different types of parameters, it also allows a different number of parameters. The number and types of parameters may be different. However, not only a function of different types and the same number and type of parameters, the number of parameters of overloaded functions, parameters or parameter type 3 by sequence must have at least one different type of function return value may be the same or different.

3、function template

       The so-called function template, in fact, is to establish a common function, a function of the type and parameter types do not specify, with a virtual type to represent. This generic function is called a function template.

Template <classs type parameter list>

Returned type function name (parameter list function models)

Function body

Wherein, class can also be used instead of typename:

template < typename T>  或  template <class T>

#include "iostream"

using namespace std;

template <typename T>

T abs(T x)

{

return x<0? -x:x;

}

int main ()

{

int n=5;

double d=-5.5;

cout<<abs(n)<<endl;

cout<<abs(d)<<endl;

return 1;

}

The following is a template class (class template) definition:

Template <template parameter list>

Class class name

 The definition of class template body;

};

如template <class T>

Template <class T,int element>

Template class <template parameter table> 1 object name, object name .... n.

If the class template member function implemented outside the class, it must be a template function. Such as:

template<class T>

T& Array<T>::operator[](int index) {

  return A[index];

Classname<int,double>obj1(86,175.3);

#include "iostream"

using namespace std;

template <class Type1,class Type2>

 class myclass

 {

private:

 Type1 i;

 Type2 j;

public:

 myclass(Type1 a,Type2 b)

 {

 i=a;

 j=b;

 }

 void show()

 {

 cout<<i<<" "<<j<<endl;

 }

 };

int main ()

{

myclass<int,double>obj1(86,175.2);

myclass<char,char*>obj2('m',"Perfect");

obj1.show();

obj2.show();

return 1;

}

Template <class T>

Class Store

{...

Void PutElement(T x);

...}

void Store<T>::PutElement(T x);

     It can be seen more easily than with a function template function overloading, the program is more succinct. It should be noted that it applies only to the case of different types of the same number of arguments, and the body functions are the same, if a different number of parameters, can not use the function template.

       Since the class template contains the type parameters, so-called parameterized classes. If the class is an abstract object, the object is an instance of the class, the class template is an abstract class, the class instance is a class template. Templates can be established using the class contain various types of data classes.

       The presentation summarized above, may declare and use class template: 

(1) First write the actual class. Because of its semantic clear, be clear, generally will not go wrong.

(2) in such ready to change the type name (e.g., to be changed to float or int char) specifies its own virtual switch type name (as in Example numtype).

(3) added in front of the class declaration line in the form 

template <class virtual type parameter>, such as

template <class numtype> // no semicolon at the end of the Bank Note

class Compare                          

{...}; // body type

(4) define the object class template with the following form:

Class template name <actual type name> name of the object;

Class template name <actual type name> name of the object (argument table columns);

Such as

Compare<int> cmp;

Compare<int> cmp(3,7);

(5) If the class member functions defined outside the template, the form template should be written as a function: 

template <class virtual type parameter>

Function type class template name <Parameter type virtual> ∷ member function name (the function parameter list column) {...}

template<class numtype>

numtype Compare<numtype>∷max( )

{{return (x>y)?x:y;}

Description: 

Parameter Type (1) a template class can have one or more, must be added in front of each class types, such as

template<class T1,class T2>

class someclass

{…};

When defining an object are substituted for the actual type name, such as

someclass<int,double> obj;

(2) and use the same class, pay attention to the scope of its use of class template, can only use it to define the object in its effective scope.

(3) the template may be hierarchical, a template class can be used as base classes, derived classes derived template.

4, there is a default function arguments

If a function declaration (definition) with a float area (float r = 6.5); // default value of r 6.5

Involved in binding the real parameter is performed from left to right. Thus the default value of the parameter must be specified in the rightmost column of the parameter table, otherwise an error.

Both as a function can not be overloaded functions, but also has a function as a default parameters. Because when the function is called if a little write parameters, the system can not determine the use of overloaded functions or use the default function arguments, appears ambiguous, the system can not perform

5, the internal functions and external functions

     If a function can only be called by other functions in this document, it is called an internal function. When defining the internal function, the function type, and function name plus static. The general format of the function header for the

static type identifier function name (parameter list)

The static int fun (int a, int b), also known as a static internal function (static) function.

      When you define a function, if preceded by the keyword extern in the leftmost function header, then this function is an external function calls available to other documents. The function can be written as header extern int fun (int a, int b)

Thus, the function fun to call it for the other files. If omitted extern defining a function, the default for the external function. In the documents need to call this function, the function declaration with extern external function is used.

6, the function prototype, pre-

The function prototype can be used to expand the scope of function definitions to a file other than the function (without having to use extern). As long as the function can contain function prototypes for each file used in this function. Function prototype tells the compiler system: the function is defined later in this document, or defined in another file. The most common example of the use of the extended function scope is the function prototype application #include command.

Pre-unified command is specified in C ++, but it is not part of the C ++ language itself, can not compile them directly. In order to distinguish the general C ++ statements, these commands with the symbol "#" at the beginning and at the end does not contain a semicolon.

C ++ provides pre-processing functions mainly in the following three kinds:

(1) macro definitions

(2) file that contains

Is the interface between the header files source files, header files in the new C ++ Standard Library is generally no longer comprises the suffix .H.

(3) Conditional compilation

But sometimes you want the program only in a certain part of the contents to be compiled only when certain conditions are met, that is, to specify conditions for part of the program compiled. If this condition is not satisfied, do not compile this part. This is the "conditional compilation."

A] 

#ifdef identifier

Block 1

#else

Block 2

#endif

B]

#if expression

       Block 1

#else

       Block 2

#endif

 

Published 208 original articles · won praise 30 · views 10000 +

Guess you like

Origin blog.csdn.net/hopegrace/article/details/104250954