How to change this c++ 'macro' to inline code?

How would you convert the macro below to inline code?

//macro...
#define CATCHERROR(ptr,a) catch(_com_error &e)\
{\
ErrorHandler(e,m_ErrStr);\
ptr=NULL;\
return a;\
}

//code snippet...
1 bool Open(char* UserName, char* Pwd,char* CnnStr)
2 {
3 try
4 {
5 HRESULT hr;
6 hr = m_Cnn.CreateInstance( __uuidof( ADODB::Connection ) );
7 m_Cnn->Open(CnnStr, UserName, Pwd, NULL);
8 }
9
10 CATCHERROR(m_Cnn,0)
11
12 sprintf(m_ErrStr,"Success");
13 return 1;
14 }

I replaced line 10 above
10 CATCHERROR(m_Cnn,0)
With...
10 catch(_com_error &e){ErrorHandler(e,m_ErrStr); return 0;}

and it seems to work......... BUT NOTICE THAT ... ptr=NULL
in the macro was simply left out of my replacement code...

Should the replacement code be...
10 catch(_com_error &e){ErrorHandler(e,m_ErrStr); e=NULL; return 0;}

This seems to compile and run... but I'm on shaky ground here learning about macro's...

So, thanks for any help from a more knowledgeable source in resolving this.
Last edited on
preprocessor macros just substitute text in your code. Just substitute the parameters ptr and a in that code. At line 10, you could replace it like that:

1
2
3
4
5
6
7
catch(_com_error &e)
{
   ErrorHandler(e,m_ErrStr);
   m_Cnn=NULL;
   return 0;
}

Thanks 'TheDestroyer" !

Somehow I got confused on the fact that 'ptr' was in fact 'm_Cnn'

The code seems to be working now and things make better sense.

And, even though I understand how macro's work... they can still be very confusing!

Any doubts? Take a look at...
http://www.cprogramming.com/tutorial/cpreprocessor.html

Thanks again for the help!
Topic archived. No new replies allowed.