Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class with pointers

Status
Not open for further replies.

chikyu

Programmer
Jul 11, 2001
6
JP
Hi everybody,

This is really a very nice forum.Congratulations to the creators and thanx to all partipants.
So my question is after having implemented a destructor like
Stream::~Stream()
{
delete [] nodes;
}

Do I need to call it in the main() function or is it automatically called by the system?

Thanx for your contribution
 
The moment you instantiate a class, the constructor is called. Classes are also scoped and have a life. After its life time (when the function ends), the destructors are called automatically (unless you use new for which delete must be called appropriately).

Hope this helps

eg

int main()
{
Stream my_stream(/*...*/); //Your constructor
//...
return 0;
}
//Destructor automatically called :) Take it Easy :)
Kartik.S
 
Just a quick question... by nodes you dont mean you have a linked list. If nodes is an array then the above is the correct way to go but if it is a linked list then the above will not clear the memory.

Matt
 
Thanx for your reply.
The class Stream is like this:
Stream{
private:
int NumOfNodes;
CPoint* nodes;
public:
Stream(int n);
...

};

somewhere I have an AllocNodes() function which does:
Cell= new CPoint[NumOfNodes];

That's why I was wondering if it's not necessary to call the destructor.

 
The problem is what are destroyed automatically only objects. If you use pointers to objects, are destroied pointers as variables, but not also the objects which they point. So, do destruct you must explicitly delete YourObject; John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top