3.8 Object Destruction

Dest.cc

#include <iostream>
using namespace std;

class X {};

class Y {
    X* p;                                                         //(A)
public:
    Y( X* q ) : p( new X(*q) ){ cout<< "constructor called\n"; }  //(B)
    ~Y(){ delete p; cout << "destructor called\n"; }              //(C) 
};

int main() {
    X* px = new X();                                              //(D)
    Y y( px );                                                    //(E)
    delete px;                                                    //(F)
    return 0;
}

Results

C:\classes\ece538\work\ch03>cl -EHsc -W4 Dest.cc
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

Dest.cc
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:Dest.exe
Dest.obj

C:\classes\ece538\work\ch03>Dest
constructor called
destructor called


Maintained by John Loomis, last updated 30 Dec 2006