Skip to main content
GameDev.net gamedev.net
🔒 Locked

Macro Hassels

Started by Wyrframe Nov 28, 2001 at 1:00 AM 3 replies 350+ views
Original Post
Wyrframe
Wyrframe
I''ve checked reference books. I''ve checked Online Help. I''ve searched about 18% of the net (or at least it feels like it...) I want to make a macro that looks like this... #define DelPtr(ptr) {delete ptr; ptr = NULL;} Now, that is what I originally thought I could do. Now, I''ve reconsidered, and I think that this will work... _inline void DelPtr(void &*ptr) { delete ptr; ptr = NULL; } Could anyone tell me how to do this? I want to make a quick little inline or macro function that will do this (Delete and NULL a pointer), but neither of these attempts work with my compiler! I''m using Borland''s 5.5 32-bit compiler, if that makes a difference...
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
strtok
strtok
Try this:

_inline void DelPtr (void **ptr)
{
delete *ptr;
*ptr = NULL;
}

Then in your code you do this:

DelPtr((void **)&some_pointer);

The compiler doesn''t like it if you don''t cast it to a (void **).
Advanced Bug
Advanced Bug
I think this problem can be solved using function templates. Write something like this:

  
template<class T>
inline void DeletePtr(T*& pT)
{
delete pT;
pT = 0;
}


int* pInt = new int(10);
DeletePtr(pInt);


T*& is reference to pointer so it is passed by reference.
Void
Void
Continue searching the net for smart pointers.

Check out www.boost.org
SteveC
SteveC
Advanced Bug''s solution is the right way to go.

delete needs to have the correct type.

that''s the reason it works as the macro - the code just gets copied in there by the preprocessor, but not as an inline without templates.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.