Solved

easy: is object created?

Posted on 2000-03-06
5
185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Suggested Courses

710 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