分享一个挺有意思的代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 struct Point {
 5     static int cnt;
 6     Point() {
 7         ++cnt;
 8         cout << "There is " << cnt << " item(s)." << endl;
 9     }
10     ~Point() {
11         --cnt;
12         cout << cnt << " item(s) remains." << endl;
13     }
14 } ;
15 int Point::cnt;
16 
17 int main() {
18     Point a, b;
19     delete (new Point());
20     Point c, d;
21     return 0;
22 }
View Code

相关文章: