Macros
#1
Posted 30 September 2003 - 03:25 AM
The Lion King</span>
#2
Posted 30 September 2003 - 03:34 AM
If yes, then here is an example:
#define sum(a,;) a+b
macros don't return any value..they simply get replaced (like doing a search and replace) with the macro definition.
#3
Posted 30 September 2003 - 04:45 AM
they can be useful, though.
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
#4
Posted 30 September 2003 - 05:09 AM
void Sum(int a, int b)
Then I can use it by the following Macro
#define SUM(i, j) Sum (i, j)
But if the function is returning an integer (or anyother value) like
int Sum(int a, int b)
How can I use macro with this function returning a value :blush: ?
The Lion King</span>
#5
Posted 30 September 2003 - 05:41 AM
also the macro does nothing more than replacing the text SUM(i, j) with Sum(i, j)
so it doesn't matter if the function returns a value or not. again, why would you want to do that ???
#6
Posted 30 September 2003 - 08:04 AM
HWND WinCreate (LPSTR Title, int Width, int Height)
{
...
...
...
return hWindow;
}
I can use the following macro for this function :
#define WINCREATE_DEFAULT(Title) WinCreate (Title, 800, 600)
The above idea can be used for a pretty handly little stuffs ... I think :blush: !
The Lion King</span>
#7
Posted 30 September 2003 - 08:36 AM
int WinCreate(char* title, int width = 800, int height = 600);
i think what you want to do will just make you code uncomprehendable...
#8
Posted 30 September 2003 - 09:58 AM
Microsofot people have used these type of Macors in DirectX.
An example is for the Color. There is one with the ARGB where we enter 4 vales, and the other is RGBA where we add only 3 values. The Alpha (A) is entered in the Macro ... :unsure:
The Lion King</span>
#9
Posted 30 September 2003 - 10:03 AM
for constants, you use enums or const, for small functions like those named colourconversions, you use inline functions. there's only ONE need for macros.. wich i won't tell you but YOU will tell ME! to show you understand them
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
#10
Posted 30 September 2003 - 10:22 AM
davepermen said:
I think it is good if we use it as follows:
#define ERRORMSG(String) MessageBox (NULL, String, "Error", MB_OK | MB_ICONERROR)
what do you think dave ... I will really love to know your suggestion :lol: :blush: !
The Lion King</span>
#11
Posted 30 September 2003 - 11:22 AM
use
inline void MsgBox(const char* string) {
MessageBox(0,string,"MsgBox without Stupid Macro",MB_OK...);
}
instead. if you really _NEED_ that.no, it is NOT the good use of macro.
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
#12
Posted 30 September 2003 - 11:33 AM
Most of people don't prefer using MACROS as functions. Is it good to use it to define values right or not? It can also be achieved by declaring variables... :confused:
But why do Microsoft people use it so much? :confused:
The Lion King</span>
#13
Posted 30 September 2003 - 05:46 PM
C programmers use macros, C++ programmers don't. A good C++ programmer would never need a macro. For defining constants he would use const.
Quote
He who knows not and knows that he knows not is ignorant. Teach him.
He who knows not and knows not that he knows not is a fool. Shun him.
#14
Posted 30 September 2003 - 08:38 PM
baldurk. there is no point in using macros and you know it. none, except one..
i have had enough troubles to learn that windows.h defines both max and min as macros.. guess how i wondered as i used the same names in my code..
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
#15
Posted 01 October 2003 - 01:25 AM
davepermen said:
But macros do come in handy sometimes. Most of the times macros are just used to make typing less though, which is not good, generally speaking. But there are times when they are used really well. For example, has anyone used the memory manager from http://www.fluidstudios.com. I dont think there is a way to accomplish what he does any other way then to use macros for new/delete
Yes there are disadvantages to dropping his mem manager in your project, but the adavantages are way more in this case.
Also there are somethings you cant do with normal functions but you can do with macros. Like the # and ## thingies (dont know what they are called exactly). I guess macros are usually used for debugging purposes. This is an amazing use of macros for example
- Me blog
#16
Posted 01 October 2003 - 01:39 AM
The Lion King</span>
#17
Posted 01 October 2003 - 04:29 AM
well, read up the post right in front of yours.. there are good examples there.
in general, macros are ONLY good if you need them. this is true for example in debugging situations, where a simple assert can get much additional info thanks to a macro. like this simple snippet:
#define assert(assertion) doAssert(assertion,#assertion,__LINE__,__FILE__)etc.. the cuj assert code is much more complex, but as well much more powerful.
once you defined assert, it acts like a keyword to the language, means you CAN NOT USE IT IN ANY OTHER WAY EVER AGAIN. and that is the worst thing of macros. as you can see by the little reminder to min and max and windows.h
overloading new and delete for memory managing/debugging purposes works well without macros, but normally, you want to have there some logging info like __LINE__ and __FILE__ in, too. so you have to use some macro-way to get them automatically in again. no big problem, because new and delete yet ARE keywords. still, it gives issues..
the last way, while not used much anymore, is the way to help doing less typing. something like a
#define var(name) ConsoleVariable name(#name) var(screenWidth); var(screenHeight); var(colourDepth); ... var(ammo); #undef var // don't forget this.. except var is a new language keyword for you wich you want to use everywhere..
then again, you could do this with a simple
std::map<std::string,ConsoleVariableStorage> consoleVariables; consoleVariables["screenWidth"] = someWidth; ... and then load them directly from some config files anyways..this would mean more runtime overhead, as you fill the map at runtime, but it means, too, less globals, no in-code management (==much more flexible, just add variables in the config file..), and, last but not least, no macro.
yes i DO (or did, now i'm coding solely in d currently) use macros. but when i use them, then the "right way". that means for example an assert macro, wich get compiled to {} if you do a releasebuilt, and a verify macro, same as assert, but as well in the releasebuilt. and THEN, i go to usertype.dat for my visual studio, and add assert and verify as keywords in there, so they get blue highlighted as every other c++ keyword does.
and _THEN_ i'm on the save side again.
the chaotic way c and c++ are built, and combined made me learn a lot about both languages, and it made me learn that they can get used and abused in EXTREME ways (check out the boost preprocessor lib.. hehehe macros pure). they teached me as well to _THINK_ while i'm coding, to _PLAN_ when i code, and always rethink, when ever i write something, if that thing can be really useful that way, or if it only fits my needs for now.
macros are very dangerous, they don't follow any c or c++ language rules. don't use them for constants, don't use them for small functions, at best, don't use them at all. then you're on the save side..
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
#18
Posted 01 October 2003 - 04:34 AM
#19
Posted 01 October 2003 - 04:44 AM
(and yes, it was because of anja that i needed to take the breath, and no i am not over it because yes i still love her and miss her very much.. but i finally got energy again to post around here..)
uhm, had a lot to do. tons of times i was out with friends and chicks (hehehe:D), i have currently two turntables and a mixer at home, so i'm more into music currently, too.. and i'm diving into d.. it starts to get great, the compiler starts to support its own language rather well, can't wait for a final one. currenlty, anubis and i have a lot of stress with the compiling/linking itself, but that'll be over soon i hope (a future compiler release..)..
if you want, there is some small code you can try from me..
on D.rar.. i call them tutorials to feel good:D
btw, to not get compiler errors, unrar it to some folder with NO spaces in the full path name.. say onto c:\downloads\d or c:\d or what ever.. and then you can just run the batch file to compile. have fun... more to come.....
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
#20
Posted 01 October 2003 - 05:18 AM
Got it dave :yes: !
I know you were busy ... there was someone else chatting on hotmail at your place :blush: !
The Lion King</span>
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












