C ++ object-oriented programming study notes (7)

Templates and exception handling

Template concept

A template is a tool to achieve code reuse mechanism, it can achieve a parameterized type, that type as a parameter.
Function templates and class templates include templates, which are configured to allow a user template class and template functions

Function template and template function

In fact function template to create a generic function, its function return type and parameter types do not specify, with a virtual type to represent, this generic function is called a function template.
When calling the function will be to replace the virtual type according to the type of template (template argument) argument in order to achieve the function of different functions.
Statement format is as follows

template<typename 类型参数>
返回类型 函数名 (模板形参表)
{
    函数体
}
或
template<class 类型参数>
返回类型 函数名 (模板形参表)
{
    函数体
}

Which, template keyword is a template declaration, which represents declare a template.
Type parameter is actually a virtual type name, and now it does not specify a particular type, but when using the function template must be instantiated.
Former type parameters need to add a keyword typename or class, the same as its role is expressed subsequent parameter is a virtual type name.

Example of template function max

template<typename T>
T mymax(T a,T b)
{
    if(a>b) return a;
    return b;
}
或
template<class T>
T mymax(T a,T b)
{
    if(a>b) return a;
    return b;
}

When using the function template typename keyword class or type of parameters to be instantiated, i.e., actual data types to replace it
will function template type parameter instantiation parameters called template argument

Note:
1. Type parameter allows multiple function template but must typename keyword (or class) preceding each type parameter definition section of the template
2. The function between the template and the template definition statement statements have not allowed statement
3. the function template may be overloaded
4. template and non-template function is a function of the same name can be overloaded, non-call order is to find a template that exactly matches a function, a function template instantiating

Class template template class

The so-called class template is actually establish a common class, the data members, member functions return type and parameter types is not particularly specified, with a virtual type represents
the basic format is as follows

template<typename 类型参数>
class 类名
{
    类成员声明
};
或
template<class T>
class 类名
{
    类成员声明
};

Which, template keyword is a template declaration, which represents declare a template.
Type parameter is actually a virtual type name, and now it does not specify a particular type, but when a class template must be instantiated.
Former type parameters need to add a keyword typename or class, the same as its role is expressed subsequent parameter is a virtual type name.

When using a template definition object class, the following form

类模板名<实际类型名>对象名[(实参表列)];

Member function templates can be defined in vitro template class, this time, if the presence of type parameters member function, the following conditions have to
1. The need for the function definition template declared before the members
2 on the function name prefix idiom " class name <parameter type> :: "

The general form is as follows:

template<typename 类型参数>
函数类型 类名 <类型参数> :: 成员函数名(形参表)
{

}

Note:
1. Before the class definition for each template, the template needs preceded statement, based upon the use of templates, the template must be laden behind class name <parameter type>
2. template class can have a plurality of parameters

Exception Handling

Common program errors are divided into two categories: error error when compiled and run when.
Procedural errors at run time referred to as abnormal, the abnormal process known as exception handling

method

C ++ for exception handling is that if an exception occurs during execution of the function, the function passed to the parent process, step by step to upload, if the last can not be processed, the end of the program
C ++ exception handling mechanism by check, thrown and caught three parts, respectively, by three statements try, throw, catch

An exception is thrown

An exception is thrown with a catch statement
formatthrow 表达式

If a certain program is abnormal, you can use the throw statement to throw an exception to the caller by the abnormal corresponding catch phrase to capture
the expression throw statement indicates the type of exception thrown exception types are represented by the type of expression
cases

int Div(int x,int y)
{
    if(y==0) throw y;
    return x/y;
}
//当y=0时,抛出int型异常
Abnormal inspection and capture

Check and capture abnormal use try and catch statements
format

try
{
    被检查的复合语句
}
catch (异常类型声明1)
{
    进行异常处理的复合语句1
}
......
catch (异常类型声明n)
{
    进行异常处理的复合语句n
}

When the compound statement is checked after the try statement it is likely to cause abnormal statements that code is called the protection section. If the expected exception may occur a certain program code, place it try, if unusual is where the throw statement throws this exception

used to capture the catch throw thrown exception, the catch clause exception handler is a compound statement, declaration section indicates the type of exception catch clause exception process type, is performed by the appropriate exception handler caught exception type cathc

Note
1. The detection of the statement or must be placed in the try block, otherwise no effect
2.try and catch must be useful compound statements enclosed in braces, the braces even if only one statement can not omit braces
3 a try statement can have multiple catch, but the whole sentence can have a try block
4. If the type is not specified in a catch clause exception information, but the use of three ellipses "...", then denotes any pharmaceutically abnormality information
5. in some cases, the throw statement may not include expressions, it will throw an exception at this time the information being processed again a supreme
6. If an exception is not caught, the system will call the system function terminate, in particular, calls abort to terminate the program

Guess you like

Origin www.cnblogs.com/springfield-psk/p/12076948.html