Wednesday, May 19, 2010

A perfect example to understand constructors and destructors

#include
using namespace std;

int c = 0;
int d = 0;

struct X {
    int i;
    X(int j = 0) : i(j) { c++; }
    ~X() { d++; }
};
 
int main(){

cout<
X x1;   
cout<
    {
    X x2;
    cout<
        {
        X x3;
        cout<
        cout<
        }
        cout<
    }
    cout<

return 0;
}

 

No comments:

Post a Comment