18. C ++-[] OPERATOR usage, differences between function objects and common functions (detailed description), 18. c Operator
In the previous chapter, I learned the string class from the 17.c++-string class (detailed description) and found that each character can be accessed through the [] overload operator.
For example:
string s="SAD";for(int i=0,i< s.length();i++)cout<<s[i]<<endl;
Next, we will write a [] overload operator to simulate the string class.
#include <iostream>#include "string.h"class string{private: char *str; int len;public: string(const char *p=0) { if(p!=NULL) { len=strlen(p); str = new char[len]; strcpy(str,p); } else { len=0; str = new char[1]; str[0]='\0'; } } char& operator [](int i) { return str[i]; } int length() { return len; }};int main(){ string s="SAD"; for(int i=0;i< s.length();i++) std::cout << s[i] << std::endl; return 0;}
Run print:
SAD
Function object
- Function object refers to this object.Function Behavior
- Function object through() Call operator DeclarationThen you canFunction to call this objectNow.
- () Call OperatorsYou can defineMultiple overload Functions
- () Call OperatorsOnlyClass member functionsOverload (the Global function cannot be used)
- Function objects are used in projects.Replace function pointer
For example, defineFunction object t:
Class Test {public: void operator () (void) // uses the () overload operator, to make the object have function behavior {cout <"hello" <endl ;}; int main () {Test t; t (); // print "hello "}
Differences between function objects and common functions
Function object
It can encapsulate its own members and other functions, so it canBetter object-oriented.
Common functions
OftenOnly have logical relationshipsAnd there is no fixed member, because after a common function is called, the content in it is destroyed, unlessGlobal VariablesBut global variables are not encapsulated.
Next, let's take an example of a common function and a function object to see the difference between the two.
The requirements are as follows:
- Use a function to obtainFibonacci SeriesValue of each item
- Every time you call the functions functionReturns a value.
- YesReuse
Common Function instances:
# Include <iostream> using namespace std; int cnt0 = 0; int cnt1 = 1; void fib_set (int n) // you can specify the Fibonacci sequence to enable fib () {cnt0 = 0; cnt1 = 1; for (int I = 0; I <n; I ++) {int tmp = cnt1; cnt1 = cnt0 + cnt1; cnt0 = tmp ;}} int fib () // calculate a value {int tmp = cnt1; cnt1 = cnt0 + cnt1; cnt0 = tmp; return tmp;} int main () {for (int I = 0; I <5; I ++) cout <fib () <endl; // print 1 ~ 5 values: Maid (0); // you can specify 0 for (int I = 0; I <5; I ++) cout <fib () <endl; // print 1 ~ again ~ 5 items so that it can reuse return 0 ;}
Run print:
1123511235
From the code above, we can see that the requirements implemented by common functions also needTwo global variablesThis is completely undesirable in large projects. If there are many modules like this in the project, how many global variables are there? AndGlobal variables can be destroyed at will, No encapsulation.
Next,Use Function objects to fulfill this requirement:
# Include <iostream> using namespace std; class Fib {private: int cnt0; int cnt1; public: Fib (int n = 0) {cnt0 = 0; cnt1 = 1 ;} void operator = (int n) {cnt0 = 0; cnt1 = 1; for (int I = 0; I <n; I ++) {int tmp = cnt1; cnt1 + = cnt0; cnt0 = tmp ;}} int operator () {int tmp = cnt1; cnt1 + = cnt0; cnt0 = tmp; return cnt0 ;}}; int main () {Fib fib; for (int I = 0; I <5; I ++) cout <fib () <endl; // print 1 ~ 5 items: fib = 0; // The number of new items is 0 for (int I = 0; I <5; I ++) cout <fib () <endl; // print 1 ~ 5 values return 0 ;}
Run print:
1123511235
From the code above, after using the function objectNo need to use global variablesNow.