Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

easy: is object created?

Posted on 2000-03-06
5
Medium Priority
?
213 Views
Last Modified: 2013-11-20
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;

0
Comment
Question by:Sesh
  • 3
5 Comments
 
LVL 4

Expert Comment

by:inpras
ID: 2590501
Check this
if (theObject)
  delete theObject;
0
 

Author Comment

by:Sesh
ID: 2590548
nope... didnt work
0
 

Author Comment

by:Sesh
ID: 2590562
nope... didnt work
0
 
LVL 3

Accepted Solution

by:
V_Bapat earned 200 total points
ID: 2590611
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
 

Author Comment

by:Sesh
ID: 2593947
thanx a lot...

i was hoping that there was some way of checking.. some operator or function, but that works fine...
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Integration Management Part 2
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question