C ++Although the technology is very fashionable, many C users want to label themselves as C ++ in the shortest time. There are a lot of books about C ++, but only those users who are lucky enough to get started will occasionally flip over. There are still a lot of hooligans wandering at the door of C ++.
This article only targets C users, preferably a very good old user. For example, he tries to use pointers when encountering the simplest problem ), through some C and better C ++, this article uses Borland C ++ 3.1) routines to introduce some knowledge about C ++, so that readers and friends can "dive deeper and deeper ", easy to use C to C ++!
4. function module function template)
The overload mechanism discussed earlier is not popular for sum operations. It seems that it is not a C ++ style. For example, the user needs to require the sum of two other types, such as struct type) objects: add 'A' 'B'); it must prepare a version for it, although its name and code are like that:
- char Add (char a char b)
- {
- return a + b;
- }
Such boring work will make discouraged users miss the old "macro ". However, the more advanced one-by-one templates can easily solve the above problems:
- template <class TYPE>
- TYPE Add (TYPE a TYPE b)
- {
- return a + b;
- };
As a template parameter, it indicates the data type. In actual calls, the compiler generates corresponding functions based on the actual data type. For example:
- int i=Add(1 2); //int Add(int int)
- float f=Add(1.0 2.0); //float Add(float float)
Will get the correct explanation of the compiler. But the following uses:
- int i=Add('A' 0. 0l);
- //error: Could not find a match for 'Add(char double)'
Of course, it will be rejected by the compiler.
The above created Add) function template can overwrite all the previous Add) functions, but let's look at the following statements:
- struct COMPLEX {float r; float i;};
- typedef struct COMPLEX complex;
- complex c1 c2;
- complex c=Add(cl c2);
Similarly, the compiler is customized:
- c=c1 +c2 };
This result is not defined, and the computer can easily be overwhelmed by the addition of the two plural numbers:
Error: Illegal structure operation
Since the computer does not like this work, it doesn't matter. We just need to make another function for it:
- complex Add(complex c1 complex c2)
- {
- complex c;
- c. r=c1. r+c2. r;
- c. i=c1. i+ c2. i;
- return c;
- }
This function is used to calculate the sum of multiple numbers correctly. It is strange that the function name can still be set to Add without worrying about any conflicts. C ++ is also a good saying about this situation. It is called "function template RESET ".
In the call form, the function template is similar to a macro, but it also has a type check. More commonly, templates can also be applied to classes.
So far, the battle against # define is coming to an end, but it seems that it will never end. Macro is a macro, which always has its advantages. For example, it can save object space, and you cannot prevent some C ++ users from enjoying it.
5. operator overload)
What I want to declare is that the Add function defined above, especially the one customized for complex, is still worthy of disdain. Although they all work normally, they are still not commonly used in C ++. Since sum is used, we prefer the expression "complex c = c1 + c2;" instead of "complex c = Addcl c2 );".
The '+' operator is much more comfortable to call than the Add function. In C ++, You can discard the so-called "template RESET" and directly reload the '+' OPERATOR:
- complex operator+(complex c1 complex c2)
- {
- complex c;
- c.r=cl.r+c2. r;
- c. i=cl.i+c2. i;
- }
This occurs. In the form of c1 + c2, the expression is given a valid meaning. The following describes the overloading of some common operators:
1) single object Operator Overloading:
If @ is set to a single object operator, @ x and x @ are interpreted as operator @ x ).
Look, isn't that the form of function call? Operator is the keyword of C ++. For example, the statement y = -- x; will be translated as y = operator -- x). The following is an example of how to evaluate the opposite number of plural numbers:
- //test11. cpp
- #include <iostream.h>
- #include "complex.h"
- complex operator - (complex c)
- {
- c.r = -c.r;
- c.i = -c.i;
- return c;
- }
- void main()
- {
- complex c={1.0 2.0};
- c= -c;
- cout<<"c=(" <<c.r<<''<< c.i <<"i)\n";
- }
Assume that the complex schema declaration is included in the complex. h header file, testl l will generate the following output:
- C = (-1-2i)
- '+ +' And '--' can also be reloaded:
- Complex operator ++ (complex & c );
- Complex operator-1 (complex & c );
- Complex c;
- C ++;
- -- C;
'+ +' And '--' are a pair of strange things. They can both be prefix and suffix. However, the following definitions only apply to the suffix usage of '+ +' and:
- complex operator++(complex&c int);
- complex operator--(complex&c int);
- complex c;
- c++;//ok
- ++c; //error. Illegal structure operation
- c++(0); //error: Call of nonfunction
Note: The operation int parameter is only used as a flag and has no other meaning.
(2) binocular Operator Overloading
Set @ to a binary operator, and x @ y is interpreted as: operator @ (x y)
Example Statement:
- z=x+y;
Translated
- z=operator+(x y);
Needless to say, the previous complex operator + (complex c1 complex c2) is a good example.
(3) Reload of new delete
New delete can also be overloaded (not to mention their mysteries). They generally adopt the following declaration form:
- void*operator new (size_t size);
- void operator delete (void*p);
The size t is an implementation-related unsigned int type. Their usage is as follows:
- int*ip=new int;
- delete ip;
When a TYPE object space is allocated using new, sizeof (TYPE) will be used as the first parameter to call the new (size_t) function. The above new statement will be translated:
- ip=operator new (sizeof(int));
The following is an example of heavy load:
- //test12.cpp
- #include <alloc.h>
- #include <iostream.h>
- #include "complex.h"
- static void * operator new (size_t size)
- {
- cout << size << " byte(s) allocated! \n";
- return malloc(size);
- }
- static void operator delete (void *p)
- {
- free(p);
- cout<<"memory block returned! \n";
- }
- void main()
- {
- int *ip = new int(10);
- complex *cp = new complex;
- float * fp = new float[10];
- delete [] fp;
- delete cp;
- delete ip;
- }
Output result:
- 4 byte(s) allocated!
- 8 byte(s) allocated!
- 40 byte(s) allocated!
- memory block returned!
- memory block returned!
- memory block returned!
In this example, malloc) and free) are picked up again, replacing the new delete function. At the same time, new) delete) function declaration is static type, to prevent their Overloading on other files produce side effects. The default new delete version is used before the new and delete versions are reloaded.
Operator Overloading is the most proud Ace, but you must remember that it still has the following restrictions: ① Operator Overloading requires that at least one operator object is a general concept that the class object class is only a structure ). I have tried the following:
- //error: 'operator+(char*char*)’ must he a member function or have a parameter of class type
- char*operator+(char*s1 char* s2)
- {
- return strcat(sl s2);
- }
However, the compiler later proved that this kind of empathy for the basic data type is stupid.
② New operators cannot be constructed, the number of operator parameters cannot be changed, and the operator priority cannot be changed.
③ The meaning of operators should be as faithful as possible to the original meaning of operators. This is not a strict rule, but a good piece of advice. For example, when you convert complex '! 'Operation is defined as the machine restart code. Although C ++ has no reason to block you, this is not good.
At this point, this series is all about. Hope to help you.