No Disconnect

Things I’d rather not forget

C++ object/pointer construction/deconstruction

leave a comment »

#include <iostream>

using namespace std;

class A
{
public:
 char label;
 A()
 {
 label = 'X';
 }
 A(char l)
 {
 label = l;
 cout << "Constructing " << label << endl;
 }
 ~A()
 {
 cout << "Deconstructing " << label << endl;
 }
};

int main()
{
 A a('a');
 A *b;
 A *c = new A('c');
 A *d = new A('d');
 delete d;

 return 0;
}

Output:

Constructing a
Constructing c
Constructing d
Deconstructing d
Deconstructing a
Advertisement

Written by nodisconnect

March 1, 2011 at 10:58 pm

Posted in C/C++, Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.