How to do this Macro ?

Started by
5 comments, last by GameDev.net 17 years, 8 months ago
I have a class I'm using to assist with debugging, and it has a method that looks something like this: void add_label ( string sourcefile, string sourceline, string label ); So, in my code I have to do: class.add_label( __FILE__, __LINE__, "before mem alloc"); Using macros, is there any way to just do: class.add_label( "before mem alloc" ); ...and have the code work?
Advertisement
I think the best you'll be able to expect is ADD_LABEL(class, "before mem alloc").
Quote:Original post by SiCrane
I think the best you'll be able to expect is ADD_LABEL(class, "before mem alloc").


ah, sorry, i don't actually need the code to be:
class.add_label( "before mem alloc" );

and "class" is a global, so the Macro should end up looking something like this:
Debug_AddLabel( "before mem alloc" );
#define Debug_AddLabel(Data) class.add_label(__FILE__, __LINE__, Data) ?
Quote:Original post by raz0r
#define Debug_AddLabel(Data) class.add_label(__FILE__, __LINE__, Data) ?


Thank You, Rating++
Macros are just textual replacements, take whatever text that you would type and put it into the macro, and the compiler will type it for you.

class Test{public:	void UseLine(unsigned int Line)	{		std::cout << "Line: " << Line << std::endl;	}};#define USELINE() UseLine(__LINE__)void main(){	Test t;	t.USELINE();	Pause();}


Works fine in MSVC. Just don't make the macro and any other function have the same name.

Edit: I see that there was an entire exchange in the time I was testing out macros-as-member functions, well, have fun.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Some code that might help:


// preprocessor - turn parameter to string#define PSYKR_STRINGIZE( text ) PSYKR_STRINGIZER( text )#define PSYKR_STRINGIZER( text ) #text// preprocessor - turn string into unicode#define PSYKR_WIDEN( a ) PSYKR_WIDEN2( a )#define PSYKR_WIDEN2( a ) L ## a// preprocessor - turn parameter to wide string#define PSYKR_WSTRINGIZE( x ) PSYKR_WIDEN( PSYKR_STRINGIZE( x ) )// then you can just get a bunch of strings like so:#define Debug_AddLabel( x ) class.add_label( PSYKR_WIDEN(__FILE__), PSYKR_WSTRINGIZE(__LINE__), x )

This topic is closed to new replies.

Advertisement