Archived

This topic is now archived and is closed to further replies.

Question pertaining to pointers

This topic is 5117 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Simple Question: Is a class's destructor called when a pointer to an objected is delete'd?

// MC.h

class MC
{
public:
  MC();
  ~MC();

  int foo();

  static int m_mcCount;
}
int MC::m_mcCount = 0;

// MC.cpp

MC::MC()
{
 m_mcCount++;
}

MC::~MC()
{
 m_mcCount--;
}

int MC::foo()
{
 return 0;
}

// main.cpp

int main()
{
  MC *myMC;
  myMC = new MC;
  MC->foo();
  delete MC;
return 0;
}
In the above example, the member m_mcCount is set to 1 when myMC allocates a new MC object (correct?). Is m_mcCount reduced to 0 when myMC is delete'd? ================== My (soon-to-be) Cherished Cookie of Appreciation: -- MattB - WinSock advice -- -- Sneftel - how 'extern' works -- -- LessBread - Hooks ans Shared Memory -- -- groby - safe assumed MTU size -- [edited by - zackriggle on December 13, 2003 1:31:10 AM]

Share this post


Link to post
Share on other sites
Well, you could do some experimentation, by placing a printf in the destructor to see if it''s called, but I''ll give the short answer: Yes, if the object was allocated with a new call, then deleting it will call the destructor too.

I like pie.

Share this post


Link to post
Share on other sites