Raised This Month: $51 Target: $400
 12% 

#define vs const (C++)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Peoples Army
SourceMod Donor
Join Date: Mar 2007
Old 02-03-2010 , 13:24   #define vs const (C++)
Reply With Quote #1

Ok, I posted my code for a HL2 mod I'm workin on, and some one mentioned that I should use #defines in place of where I had my const set. It was a string and some integers I was setting as the mods default values.

I searched google for a few hours reading debates, and never seemed to get a definitive answer, so I was wondering if anyone could offer there knowledge please & thank you

I haven't got that far in my school's C++ book yet
__________________
Peoples Army is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 02-03-2010 , 13:42   Re: #define vs const (C++)
Reply With Quote #2

Quote:
Originally Posted by Peoples Army View Post
Ok, I posted my code for a HL2 mod I'm workin on, and some one mentioned that I should use #defines in place of where I had my const set. It was a string and some integers I was setting as the mods default values.

I searched google for a few hours reading debates, and never seemed to get a definitive answer, so I was wondering if anyone could offer there knowledge please & thank you

I haven't got that far in my school's C++ book yet
I hate defines. They make the code look uglier, they are not programming and when they are multi line defines they piss me off. The advantages of using them in comparison to inline functions is that you have guarantee that they are inlined. So the place where they are useful is when you have a function used several times (what several times mean depends on the situation) and want to avoid calls to it.

When you have stuff like "#define x 1+3" you shouldn't be using defines because the sum will occur on every replacement and if you use "const x = 1 + 3" it will happen once at compile time.

I can't say, even when they make things faster, if they are recommendable because if the thought is to make it faster without caring with code becoming much worst to read, why not make functions in assembly also?

An example on why I don't like defines and that is a situation that happened to me:

Having a "#define max ...", code with a class that contains a method named, "max" will not compile.
joaquimandrade is offline
Peoples Army
SourceMod Donor
Join Date: Mar 2007
Old 02-03-2010 , 15:23   Re: #define vs const (C++)
Reply With Quote #3

I agree but I've already changed my code 5 times after each time I read a debate lol.

I think the way I'm watching valve do it defining a model string & path might be correct but I'm not sure, I don't think I'll ever need to get a reference from it or anything like that so it might be safe, Ive just heard const is type safe too which I'm not sure if that matters here or not.
__________________
Peoples Army is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:36.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode