Solved

easy: is object created?

Posted on 2000-03-06
5
160 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now