View C ++ from the perspective of assembly (Template Class)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

If a class is a specific data type, the template is an abstraction of the class. If there is such a type that requires data computing and there are still many types, we may need to define different classes for different types of data. We can use the following code to describe the problem:
class int_process{int a;int b;public:int_process(int m, int n):a(m), b(n) {}~int_process() {}int add() {return a + b;}int sub() {return a - b;}int mul() {return a * b;}int div() {return a / b;}};class short_process{short a;short b;public:short_process(short m, short n):a(m), b(n) {}~short_process() {}short add() {return a + b;}short sub() {return a - b;}short mul() {return a * b;}short div() {return a / b;}};

The above code is actually relatively simple and you can understand it. The first class is int_process, which is mainly used to calculate integer addition, subtraction, multiplication, and division. The second type is short_process, which is mainly used to calculate the addition, subtraction, multiplication, and division of short integers. The content of the two classes is actually very similar. Is there a simple way to process both classes at the same time? Yes! This is the template. We can abstract specific data types to form a new class mode. This is the template class. The following code is a template class:

template <typename type>class data_process{type a;type b;public:data_process(type m, type n):a(m), b(n) {}~data_process() {}type add() {return a + b;}type sub() {return a - b;}type mul() {return a * b;}type div() {return a / b;}};

We can see that classes abstract specific data types into types. At this point, no matter the input value or the output value, we have changed to type. As for the class name, we also changed from the original specific data type calculation to the general data_process. Of course, the definition of this name is not very important. How should we apply the template class after it is defined? Continue to read the code:

void process(){data_process<int> d(1,2);data_process<char> m('1', '2');data_process<double> p(1.2, 2.3);}

As you can see from the code above, the definition of the template class is not complicated. Just add the specific data type after the name of the template class. If the data type is int, the int data is processed. Similarly, if the data type is Char or double, we can calculate the data type according to Char or double, which is very convenient. The processed data is far more than the char, double, float, Int, short, and long data types defined in the C ++ language. If the type is a class type, at the same time, the class type also supports +,-, *, And/Operations, so it can also be used as a template. Here we will introduce int, Char, and double simply to illustrate the problem. When we see the Declaration of classes, we can't help wondering, since there is only one template class, are the constructors, destructor, and member functions of these template classes processed in the same way? Let's take a look at their assembly code:

60:       data_process<int> d(1,2);0040126D   push        20040126F   push        100401271   lea         ecx,[ebp-14h]00401274   call        @ILT+45(data_process<int>::data_process<int>) (00401032)00401279   mov         dword ptr [ebp-4],061:       data_process<char> m('1', '2');00401280   push        32h00401282   push        31h00401284   lea         ecx,[ebp-18h]00401287   call        @ILT+55(data_process<char>::data_process<char>) (0040103c)0040128C   mov         byte ptr [ebp-4],162:       data_process<double> p(1.2, 2.3);00401290   push        40026666h00401295   push        66666666h0040129A   push        3FF33333h0040129F   push        33333333h004012A4   lea         ecx,[ebp-28h]004012A7   call        @ILT+60(data_process<double>::data_process<double>) (00401041)004012AC   mov         byte ptr [ebp-4],263:       int i_d = d.add();004012B0   lea         ecx,[ebp-14h]004012B3   call        @ILT+70(data_process<int>::add) (0040104b)004012B8   mov         dword ptr [ebp-2Ch],eax64:       char c_m = m.add();004012BB   lea         ecx,[ebp-18h]004012BE   call        @ILT+80(data_process<char>::add) (00401055)004012C3   mov         byte ptr [ebp-30h],al65:       double d_p = p.add();004012C6   lea         ecx,[ebp-28h]004012C9   call        @ILT+75(data_process<double>::add) (00401050)004012CE   fstp        qword ptr [ebp-38h]66:67:   }
The above code is a bit long. Let's take a look at it: 60 sentences: Define the int type class. We can see that the data_process <int> constructor address is 0x401032 61 sentences: define the class type of the char type. See data_process <char> the constructor address is 0x40103c. Sentence 62: Define the class type of the double type, the data_process <double> constructor address is 0x401041 63 sentences: Call the Add member function of data_process <int>, and the address is 0x40104b 64 sentences: call the Add member function of data_process <char>. The address is 0x401055 65 sentences: Call the Add member function of data_process <double>. The address is 0x401050. The code above indicates that, in fact, the compiler instantiates each specific class in our function. For each type, the template constructor, destructor, and member functions must be generated independently. From the above function address, we can see that there is no magic. Therefore, we understand that the essence of a template is to extract common attributes and synthesize templates for similarity operations of different data types. During the application, the compiler independently generates each Class Based on the Data Type in use and constructs each basic calculation variable and function. That's all. Template considerations:(1) problems with the class will occur in the template class (2) first write the class and then convert it into the template class (3) if it is not a data type difference, however, there is a difference in the total number of data. Please use inheritance instead of template (4) the type in the template can be a custom type (5) the template code can only appear in the header file, appears in *. CPP file is meaningless, separate *. the CPP template Code does not involve specific types, so it will not be compiled into any binary code (6) different versions of VC have different template support. compilation errors are not necessarily your own reason, but most of them should be your cause (7) the alert generated by the template is lengthy, with a warning or error 30 ~ 50 rows are normal. Don't be afraid. [Notice: The following blog introduces template functions]

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.