C++ Middle front ++ And postposition ++ It's different , Specific differences , It is shown in the following code .
At the back ++ in , Add a parameter manually (int), The main purpose is to distinguish the front ++, This parameter is not used . there int It's a dumb guy .
Parameters of a function Only type No name Then this parameter is called dummy .
class A { public: A& operator++()// Front ++, The reference is returned { data +=1; return *this; }
const A operator++(int)// Postposition ++, The value is returned { A old(*this); ++(*this); // Call before ++ return
old; } // As you can see from the code , Front ++ Compared with the post ++ efficient , Do not generate temporary objects , Do not call the copy constructor int data; }; ostream&
operator<<(ostream& os,A& a) { os<<a.data<<endl; return os; } int _tmain(int
argc, _TCHAR* argv[]) { A a={1}; cout<<a;//1 A b=++a; cout<<b;//2 cout<<a;//2 A
c=a++; cout<<c;//2 cout<<a;//3 return 0; }
Technology
Daily Recommendation