Advertisement

#define vs const

Started by March 22, 2001 07:02 PM
23 comments, last by thuned 23 years, 10 months ago
Cyberdrek, couldn''t you use templates to solve that though?

"You don''''t get wise with the sleep still in your eyes." - Neil Peart
"You won't get wise with the sleep still in your eyes." - Neil Peart

Yes, that's one of the things templates do best!


template
inline const T& min(const T& a, const T& b) { return (a < b) ? a : b; }

template
inline const T& max(const T& a, const T& b) { return (a > b) ? a : b; }


These functions are part of the C++ standard library algorithms - why bother defining them yourself?


Edited by - null_pointer on March 24, 2001 7:37:34 AM
Advertisement
I''ll believe that inline functions are better when somebody can give me the inlined equivalent of this simple macro, which I use in every single project I work on:

#define SafeRelease(p) { if ( (p) ) { (p)->Release(); (p) = 0; } } 


C''mon, just gimme that inline function, then I''ll believe you guys. Honest.

-----------------
The Goblin (madgob@aol.com)
-----------------
"Before critisizing somebody, walk a mile in their shoes. That way, when you do critisize them, not only will you be a mile away, but you''ll also have their shoes!"
- The Goblin (madgob@aol.com)
  inline template <class P> void SafeRelease(P obj) {  if( (obj) ) {    (obj)->Release();    (obj) = 0;  }}  

I think that should work. I''m not saying that templates or inlines are better, but they do have their own applications.

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://www.gdarchive.net/druidgames/

You asked for it.


template &lttypename T>
inline void safe_release(T*& p)
{ if( p ) { p->Release(); p = 0; } }



Now remember your part of the bargain.


Null and Void: That won't work, you're setting a temporary copy of the pointer to 0, not the real pointer. Also, the parentheses aren't necessary because you are dealing with a real variable in a real function.

Edited by - null_pointer on March 25, 2001 8:28:55 AM

This topic is closed to new replies.

Advertisement