C + + programming language Notes (4) __c++

Source: Internet
Author: User

54 The function overload and the generic mechanism are called compile-time polymorphism, the Run-time polymorphism is defined as a virtual function in the base class, the virtual function is redefined in a derived class, and a virtual function is invoked through a base class pointer (or a base class reference);

55 In addition to the constructor, any non-static member functions can be designed to be virtual functions as needed.

56 When the pointer calls the virtual function of the class, the pointer is the object (base class or derived class) of the class to which the dynamic binding is directed.

57 invoking virtual functions through base class references also uses dynamic binding. For example: void Printidentity (base& obj);

58 The static type of pointers and references can be different from dynamic types, which is the cornerstone of C + + to support polymorphism.

59 Virtual destructor: If the pointer's static type is pointing to the base class, calling a Non-virtual destructor in a derived class calls the base class's destructor instead of the derived class's destructor, which can cause incorrect object undo operations. For example:

Base *ptr;

ptr = new DClass;

...//

Delete ptr;

60 The pure virtual function is to add = 0 to the declaration of the general virtual function. is called an abstract class that contains pure virtual functions.

61 abstract classes have the following characteristics:

Can only be used as a base class for other classes;

cannot be used to create object instances directly;

cannot be used as a parameter type and a return value type for a function;

Cannot be used to force type conversions;

You can declare pointers and references for abstract classes;

62 if the derived class does not redefine all the pure virtual functions in the class, the derived class is also an abstract class.


) try{}....catch{...} Throw

The Auto_ptr class is used to provide security for exceptions for dynamically allocated objects, so that memory leaks can be avoided even in the event of an exception. Auto_ptr is a template class that creates objects in the following three ways:

Auto_ptr<t> ap;

Auto_ptr<t> ap (PTR);

Auto_ptr<t> AP2 (AP1);

The Auto_ptr class can only be used to manage a single object returned by the new operation and cannot manage dynamically allocated arrays.

For example, a new instance of the class, the Auto_ptr management of the object, when the function terminates abnormally, auto_ptr can undo the dynamically assigned class object and invoke the corresponding destructor.


The templates in C + + include class templates and function templates.

Templeate <typename t>

return value type function name (form parameter list)

{

function Body Statement

}

67 The function template is instantiated without a general implicit type conversion.

68 Custom Namespaces:

Namespace name Space name {

A member of a name space

}

69 function templates can also be overloaded, in the context of the given real parameter matching (full) of the overloaded function, that is, static binding, otherwise use the template to determine whether the match, and also with the implicit conversion of overloaded functions to match.

70 The main purpose of the class template is to define the container data structure.

71 because the template parameters of the object are used when instantiating the class template member function, the function argument is allowed to be implicitly converted when the class template member function is invoked.

72 There are two modes of template compilation: including compilation mode and separating compilation mode;

73 contains the compilation mode: the template is defined in the header file, the template is implemented in the other. cpp file, but the implementation file is included with the preprocessing directive #include in the header file.

74 Separate the header file (. h) and the implementation file (. cpp) is a generic implementation file containing a header file, not an implementation file, as in the case of compilation mode, but a class template declaration is added to the implementation file to indicate that the template is exported, for example:

Export Template <typename t> class Stack;

#include "GenericStack.h"

75 non-type template parameters: Also consists of type and formal parameter names, when the template is instantiated, the untyped parameters are replaced by the values of the corresponding template arguments.

Cases:

Template <typename T, std:size-t n>

void Printvalues (T (&arr) [N])

{

for (std::size_t i = 0; I!= N; ++i)

Std::cout << Arr[i] << Std::endl;

}

int intarr[6] = {...};

Printvalues (intarr);

Example 2:

Class template ... Stack<int, 10> stack;

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.