Jump to content


Macros


29 replies to this topic

#1 TheLionKing

    Valued Member

  • Members
  • PipPipPip
  • 143 posts

Posted 30 September 2003 - 03:25 AM

I was just studying Macros and they are pretty useful ... Just want to know how to create a Macro to a function that returns a value? :blush:
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

#2 Dia

    DevMaster Staff

  • Administrators
  • 1089 posts

Posted 30 September 2003 - 03:34 AM

Are you talking about C/C++ macros?

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 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 30 September 2003 - 04:45 AM

hm.. macros are evil.. just google for that:D

they can be useful, though.
davepermen.net
-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 TheLionKing

    Valued Member

  • Members
  • PipPipPip
  • 143 posts

Posted 30 September 2003 - 05:09 AM

I meant if I have a function like the one below :

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: ?
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

#5 anubis

    Senior Member

  • Members
  • PipPipPipPip
  • 2225 posts

Posted 30 September 2003 - 05:41 AM

first of all... why would you want to do this ???
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 ???
If Prolog is the answer, what is the question ?

#6 TheLionKing

    Valued Member

  • Members
  • PipPipPip
  • 143 posts

Posted 30 September 2003 - 08:04 AM

for example ... take a look at the following code :

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: !
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

#7 anubis

    Senior Member

  • Members
  • PipPipPipPip
  • 2225 posts

Posted 30 September 2003 - 08:36 AM

erm, there is such a thing as standard parameter values...

int WinCreate(char* title, int width = 800, int height = 600);

i think what you want to do will just make you code uncomprehendable...
If Prolog is the answer, what is the question ?

#8 TheLionKing

    Valued Member

  • Members
  • PipPipPip
  • 143 posts

Posted 30 September 2003 - 09:58 AM

I want to do this to add different values for rwidth & height like 600 x 400, 800 x 600 or 1024 x 800.

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:
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

#9 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 30 September 2003 - 10:03 AM

does that make it good? no. learn how to use makros and you'd realise you DON'T use it for that.

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
davepermen.net
-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 TheLionKing

    Valued Member

  • Members
  • PipPipPip
  • 143 posts

Posted 30 September 2003 - 10:22 AM

davepermen said:

there's only ONE need for macros.. wich i won't tell you but YOU will tell ME! to show you understand them
:huh: You just sond like my teacher ... :blink:

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: !
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

#11 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 30 September 2003 - 11:22 AM

what do you gain from this?

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.
davepermen.net
-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 TheLionKing

    Valued Member

  • Members
  • PipPipPip
  • 143 posts

Posted 30 September 2003 - 11:33 AM

:yes: OK Dave, I got your point.

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:
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

#13 baldurk

    Senior Member

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 30 September 2003 - 05:46 PM

I'd rather create a macro for the MessageBox example than an extra function, but here is the important point.

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

But why do Microsoft people use it so much?
I'm sorry if this surprises you, but Microsoft don't necessarily write good or clean code.
baldurk
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 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 30 September 2003 - 08:38 PM

the win32 api is from the times before you even all knew programming exists:D it is old, designed for c, and all..

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..
davepermen.net
-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 bladder

    DevMaster Staff

  • Moderators
  • 1057 posts

Posted 01 October 2003 - 01:25 AM

davepermen said:

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..
ooh. I remember those days. Damn windows.h drove me insane. Took me a while to figure out that you had to #define NOMINMAX before including windows.h.

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

#16 TheLionKing

    Valued Member

  • Members
  • PipPipPip
  • 143 posts

Posted 01 October 2003 - 01:39 AM

dave, what do you think Macros are actualy used for? :7
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>

#17 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 01 October 2003 - 04:29 AM

looks like you don't inform yourself well, do you?

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..
davepermen.net
-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 Noor

    Senior Member

  • Members
  • PipPipPipPip
  • 503 posts

Posted 01 October 2003 - 04:34 AM

Good to see you back davepermen. where have you been? Long time no see.
"What ever happened to happily ever after?"

#19 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 01 October 2003 - 04:44 AM

i needed to take a breath.. i was always here, just not posting..

(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.....
davepermen.net
-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 TheLionKing

    Valued Member

  • Members
  • PipPipPip
  • 143 posts

Posted 01 October 2003 - 05:18 AM

Wooooooowwwwwwwwww! :D

Got it dave :yes: !

I know you were busy ... there was someone else chatting on hotmail at your place :blush: !
<span style='color:blue'>I can survive anything ... even NUKES!!!
The Lion King</span>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users