Discussion:
Silly #define question
(too old to reply)
Jack
2009-08-25 07:48:47 UTC
Permalink
If I #define MakePtr(cast, ptr, castValue) (OutputDebugString("MakePtr\n");
((cast)(DWORD_PTR)(ptr) + (DWORD_PTR)(addValue)))
, this doesn't compile....
The statement is in one line to the end.
When I remove OutputDebugString, it compiles....
Is it the only way to modify it and make that a function?
Thanks
Jack
Alex Blekhman
2009-08-25 08:00:38 UTC
Permalink
Post by Jack
If I #define MakePtr(cast, ptr, castValue)
(OutputDebugString("MakePtr\n"); ((cast)(DWORD_PTR)(ptr) +
(DWORD_PTR)(addValue)))
, this doesn't compile....
The statement is in one line to the end.
When I remove OutputDebugString, it compiles....
Is it the only way to modify it and make that a function?
If you write a code like this:

int* p1 = MakePtr(int, p2, 3);

then obviously it won't compile. In order to understand what
happens just replace the macro with its text to see the problem by
yourself.

HTH
Alex
Jack
2009-08-25 08:08:44 UTC
Permalink
Post by Alex Blekhman
Post by Jack
If I #define MakePtr(cast, ptr, castValue)
(OutputDebugString("MakePtr\n"); ((cast)(DWORD_PTR)(ptr) +
(DWORD_PTR)(addValue)))
, this doesn't compile....
The statement is in one line to the end.
When I remove OutputDebugString, it compiles....
Is it the only way to modify it and make that a function?
int* p1 = MakePtr(int, p2, 3);
then obviously it won't compile. In order to understand what happens just
replace the macro with its text to see the problem by yourself.
Hi Alex,
So my question is how can I keep MAKEPTR a macro but still using
OutputDebugString in between...
according to you, that's not possible, is that?
(because the macro would expand to
int i = (OutputDebugString("MAKEPTR"); (cast)....
Are there any tricks that we can make it happen?
Thanks
Jack
Ulrich Eckhardt
2009-08-25 08:26:33 UTC
Permalink
Post by Jack
(because the macro would expand to
int i = (OutputDebugString("MAKEPTR"); (cast)....
Are there any tricks that we can make it happen?
Look up the comma operator in your favourite C or C++ textbook.

Oh, before I forget it: Shame on you for writing such fugly code in the
first place! I wouldn't say this qualifies for any definition of clean..

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Jack
2009-08-25 09:21:59 UTC
Permalink
Thanks Ulrich
Jack

Scot T Brennecke
2009-08-25 08:06:53 UTC
Permalink
Get rid of the outside set of parentheses. That's not valid C++ code to surround whole statements in parentheses.
If I #define MakePtr(cast, ptr, castValue) (OutputDebugString("MakePtr\n"); ((cast)(DWORD_PTR)(ptr) + (DWORD_PTR)(addValue)))
, this doesn't compile....
The statement is in one line to the end.
When I remove OutputDebugString, it compiles....
Is it the only way to modify it and make that a function?
Thanks
Jack
Jack
2009-08-25 08:14:43 UTC
Permalink
Post by Scot T Brennecke
Get rid of the outside set of parentheses. That's not valid C++ code to
surround whole statements in parentheses.
Hi scot,
Thanks for the reply. It seems like the Macro is trying to receive
OutputDebugString returned value.... :(
So it doesn't work.....
Thanks once again
Jack
David Webber
2009-08-25 08:31:47 UTC
Permalink
Post by Jack
If I #define MakePtr(cast, ptr, castValue)
(OutputDebugString("MakePtr\n"); ((cast)(DWORD_PTR)(ptr) +
(DWORD_PTR)(addValue)))
, this doesn't compile....
The statement is in one line to the end.
When I remove OutputDebugString, it compiles....
Is it the only way to modify it and make that a function?
inline functions have a lot of advantages.

Look at Alex's message to see the difference in this case.

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
Jack
2009-08-25 09:21:41 UTC
Permalink
Ah... Yeah...
Inline functions, just forgot
Thanks
Jack
Continue reading on narkive:
Loading...