constructors & destructors

Started by
3 comments, last by blue_harvester 21 years, 11 months ago
I know that a constuctor comes in to play when a new object is created, e.g. ''object1 = new Object;''so that this object''s data values will be set to default values. When does the destructor come into use?
Advertisement
When the object goes out of scope or is deleted using "delete" in conjuction with "new".
Perhaps you should read some material on the internet or in books about C++

As for your question...
Yes the constructor is called when an instance of a class is created
The destructor, as its name suggests is called when the instance is destroyed.

For example

class SomeClass
{
public:
SomeClass();
~SomeClass();
}

void SomeFunction()
{
SomeClass Instance;

// do something
}


When SomeFunction finishes Instance goes out of scope and is destroyed. The destructor is called then.

If you had a pointer to a class and deleted it

SomeClass *SomePointer;

// code

delete SomePointer;


The destructor would be called then as well.

Basically you use the destructor for when a class contains pointers and other data that needs to be released when the class is destroyed.

BTW Please please PLEASE READ SOME BOOKS ON CPP
--------------------------Insert witty comment here!--------------------------
A classic example taken from some C++ book:


    #include <iostream.h>class foo{public:	foo(char* name_of_instance)	{		cout << name_of_instance << " ctor running" << endl;		name = name_of_instance;	}	~foo()	{		cout << name << " dtor running" << endl;	}	char* name;};void useless(){	foo* bar = new foo("bar3");	delete bar;}int main(){	foo bar("bar");	bool dummy = true;	if(dummy)	{		foo bar("bar2");		useless();	}	foo lastbar("bar4");	return 0;}    


This will give you the output
bar ctor runningbar2 ctor running   // now inside the braces of the if-statementbar3 ctor running   // called the procedure "useless"bar3 dtor running   // "useless" ends and deletes bar3bar2 dtor running   // bar2 goes out of scopebar4 ctor running   // bar 4 declaredbar4 dtor running   // prog ends. Both bar and bar4bar dtor running    // goes out of scope  


Then you can see that the ctor(constructor) is called once for every instance of the class foo we declare. The dtor(destructor) for each of those instances (except "bar3") is then called when that object goes out of scope. You can see this, as the foo object "bar2" only exists between the braces in the if-statement. The "bar3" object calls it's destructor when the code deletes the foo* pointer. Finally the "bar" object calls it's destructor when the program ends. Any book on C++ will explain this better than I'm able to.

[EDIT] claryfied some of this and corrected the usual tag-errors
Wannabe
warning C4786: identifier was truncated to '255' characters in the debug information

[edited by - Rickmeister on April 30, 2002 7:37:09 PM]
quote:Original post by Rickmeister
warning C4786: identifier was truncated to ''255'' characters in the debug information

#pragma warning( disable : 4786 4503 )

I wish <iostream.h> would just die once and for all...

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!

This topic is closed to new replies.

Advertisement