Solved

easy: is object created?

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 82
repeatSeparator  java  challenge 13 56
string initialization in java 11 103
Hibernate methods 2 58
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

16 Experts available now in Live!

Get 1:1 Help Now