easy: is object created?

how do i check if an object has been created ??
i.e.

CMyObject *theObject;

...
if (...){
  theObject = new CMyObject;
}

delete theObject; // CANT kill object if not been created with 'new'
...

i need something like:
if ( HasBeenCreated(theObject) )
   delete theObject;

SeshAsked:
Who is Participating?
 
V_BapatConnect With a Mentor Commented:
CMyObject *theObject;
theObject = NULL;

// Create the object depending on some condition.
if (...){
  theObject = new CMyObject;
}

// Delete the object
if (theObject != NULL)
  delete theObject;

Since you set the object to NULL initially, delete will be called only if it has been created.

Hope this helps.

Vicky
0
 
inprasCommented:
Check this
if (theObject)
  delete theObject;
0
 
SeshAuthor Commented:
nope... didnt work
0
 
SeshAuthor Commented:
nope... didnt work
0
 
SeshAuthor Commented:
thanx a lot...

i was hoping that there was some way of checking.. some operator or function, but that works fine...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.