In the last chapter 17.C++-string string class (detailed) After learning the string class, we 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, let's 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:
S
A
D
Function object
- Function object means that the object has the behavior of function
- Function object, which is declared by () call operator, can be called by function
- () call operators can define multiple overloaded functions with different parameters
- () call operators can only be overloaded by a class's member function (not by a global function)
- Function object is used to replace function pointer in Engineering
For example, define a function object t:
class Test{ public: void operator () (void) //adopt()Overloading Operator ,To make an object behave as a function { cout<<"hello"<<endl; } }; int main() { Test t; t(); //Printing"hello" }
Difference between function object and ordinary function
Function object
It can encapsulate its own members and other functions, so it can be better object-oriented
Ordinary function
Usually it has only logical relations and no fixed members, because once the ordinary function is called, its contents will be destroyed, unless the global variable is used, but the global variable does not have encapsulation
Next, let's take an example of ordinary functions and function objects to see the difference
The requirements are as follows:
- Through a function, get the value of each term of Fibonacci sequence
- Every time a function is called, a value is returned
- Can be reused
Common function instance:
#include <iostream> using namespace std; int cnt0=0; int cnt1=1; void fib_set(int n) //What is the Fibonacci sequence,send fib()Can be reused { 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 Item value fib_set(0); //From new setting item digit 0 for(int i=0;i<5;i++) cout<<fib()<<endl; //Print 1 again~5 Item value,Make it reusable return 0; }
Run print:
1 1 2 3 5 1 1 2 3 5
It can be seen from the above code that two global variables are needed for the requirements realized by ordinary functions, which is totally undesirable in large projects. If there are many modules like this in projects, how many global variables are needed? And these global variables can be destroyed without any encapsulation
Next, the function object is used 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 Item value fib=0; //Set the number of items from new to 0 for(int i=0;i<5;i++) cout<<fib()<<endl; //Print 1~5 Item value return 0; }
Run print:
1 1 2 3 5 1 1 2 3 5
As you can see from the above code, you don't need to use global variables after using function objects