Not really - NULL is a constant, but its use in general programming practice can be temporary.NULL is constant, not temporary.
Not really - NULL is a constant, but its use in general programming practice can be temporary.NULL is constant, not temporary.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
party pooper... if you think about it, NULL (along with all other 'constant' variables) is temporary, because it only exists as long as the program is runningOriginally Posted by quzah
and no, I'm not talking about NULL as a universal thing, just as it applies to a C/C++ program.
more fun:Code:#include<iostream> #undef NULL int main() { #ifdef NULL int*ptr=NULL; std::cout<<"NULL defined as "<<ptr<<std::endl; #else std::cout<<"NULL not defined"<<std::endl; #endif return 0; }
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
There aren't any white space rules other than the usual. The only "newline" issue is that you escape multi-line macros. Like so:And...aren't there rules about white space, new lines, commas, semi-colons, etc. when defining macros? Could somoene explain these a little? Isn't it that you never use semi-colons, comments, or double-quotes?
The last line doesn't have a \, all others do.Code:#define FOO(bar) line1 \ line2
A macro is simply a compile time text subsutution. The ## macros smash things together. Like so:
Then you do something like this:Code:#define FOO(bar,baz) bar ## baz int var1, var2, var3 FOO( var, 1 ) = 10;They're multi-line just for the hell of it. They don't need to be. The only thing about white space is you need your #define or #include or what not to start on the left margin. Some compilers will let you get away with it indented for some of the things, but IIRC, th're supposed to start on the left most character.Code:#include<iostream> #define FOO(bar,baz) bar ## \ baz #define BAR(foo,baz) foo # \ baz int main( ) { int var1, var2, var3; FOO( var, 1 ) = 10; FOO( var, 2 ) = 20; FOO( var, 3 ) = 30; std::cout << BAR("var",1) << " is: " << FOO( var, 1 ) << std::endl; std::cout << BAR("var",2) << " is: " << FOO( var, 2 ) << std::endl; std::cout << BAR("var",3) << " is: " << FOO( var, 3 ) << std::endl; return 0; }
Code:#include <GOOD>Code:#include <bad>
Quzah.
Last edited by quzah; 07-17-2005 at 06:41 AM.
Hope is the first step on the road to disappointment.
NULL is a constant. You've just said so. Your use of it is temporary, but NULL remains constant. There's no two ways about it.Originally Posted by laserlight
Waxing philisophical...
Quzah.
Hope is the first step on the road to disappointment.
We're just playing with languageNULL is a constant. You've just said so. Your use of it is temporary, but NULL remains constant. There's no two ways about it.
I didnt say NULL is a temporary, I said NULL is temporary.
I didnt say NULL is constant, I said NULL is a constant.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Macros, if used correctly and documented correctly, can save a lot of work. And no, if the macro isn't simple, it cannot be replaced by an inline function.
Challenge 1: Write an inline function that does the same as
orCode:#define PRINT_NAME_AND_NUMERICAL_VALUE( name ) printf("%s is %d", #name, name );
Looks ugly for a little class, but write a class with 100 variables and accessors and we will see.Code:#define DECLARE_VARIABLE_AND_ACCESSORS( name ) int m_i##name; \ int GetNumber##name() { return m_i##name; }\ void SetNumber##name( int val ) { m_i##name = val; }
Or write an inline function, that will be compiled conditionally
This might be the job of a profiler... but you don't want to have a profiler on your production system.Code:#ifdef _PERF_ MEASURE_PERFORMANCE( statement, description ) [performance measuring and output code] #else MEASURE_PERFORMANCE( statement, description ) statement; #endif
I don't see any alternatives to Macros. You probably frown upon them while others get work done using them
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
Actually the first macro:
Wouldn't be to hard - not that I've tried - as you can surely use a stringstream to get the numerical value of name? You can use a stringstream to write an itos function.Code:#define PRINT_NAME_AND_NUMERICAL_VALUE( name ) printf("%s is %d", #name, name );
Actually I'm going to try that now, I'll post it up if I have any joy.
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry