Hard Light Productions Forums
Modding, Mission Design, and Coding => FS2 Open Coding - The Source Code Project (SCP) => Topic started by: Firgeis on April 29, 2002, 01:47:53 pm
-
Pretty self explanatory
Whats the code doing here?:
Assert(objp->type == OBJ_SHIP);
and here whats in the parenthesis?:
#define AIF_FORMATION_WING (1 << 0)
-
Asserts are double-checks to make sure that something that ought to be true really is. If for some reason it isn't the program should crash with a file, line number and in some cases the function that the assert failed in. Furthermore, in most cases Assert() is controlled by a define (e.g. #define DEBUGMODE) which, when not set, will make the assert function into an empty function, thereby saving processor time in a release version.
-
and 1 << 0 is a binary shift to the left. basically, the shift inserts 0s on the right of the 1, in this case 1 << 0 would be 1, 1 << 1 would be 2 (10 base 2)
-
Thanks alot for the help, still got some questions, in:
(objp->type == OBJ_SHIP); whats the -> for,? or is it a part of the name of the class/object/function
-
Originally posted by Firgeis
Thanks alot for the help, still got some questions, in:
(objp->type == OBJ_SHIP); whats the -> for,? or is it a part of the name of the class/object/function
"->" is used to gain access to members of an object/class in C.
In VB/Delphi it would look like this:
if objp.type = OBJ_SHIP then
-
Originally posted by Firgeis
Thanks alot for the help, still got some questions, in:
(objp->type == OBJ_SHIP); whats the -> for,? or is it a part of the name of the class/object/function
Umm. Not to be disrespectful, but you should get a decent book on C, or you'll be hopelessly lost wading through the 300K+ lines of code. Kernigan and Ritchie wrote the classic "The C Programming Language," and that's a good place to start -- there are other, more recent ones that are probably just as good.
C, and even more so, C++, does not have the friendliest syntax in the world, and making changes without understanding what's happening can be very dangerous (especially with pointer variables, like "objp" in your example).
As has been mentioned already, -> gives you access to the elements of a pointer to a struct or class. It combines the dereference operator ("*") with the element-of operator ("."), so "(*objp).type" is essentially the same as "objp->type"
-
You are probably right Peguin, however i did a 1 year course of Turbo C (also im programmer of other laguagues, like VB), so everything looked familiar, but well... i dont know alot of object oriented programming in C++ so ill do what i can
-
Originally posted by neimad
"->" is used to gain access to members of an object/class in C.
In VB/Delphi it would look like this:
if objp.type = OBJ_SHIP then
only if the struct/class/union is a pointer, otherwise its a dot
object *pobj;
object obj;
obj.var1=0;
pobj->var1=0;
-
The C/C++ syntax is not very good in my opinion; it requires way too many unnecessary keystrokes and has some really strange stuff assigned to certain operations. (for example, why the heck does = mean "set value to" and == mean "determine equality"?)The problem is that just about every language uses that same syntax, or a very slight variation of it. :p
-
Originally posted by CP5670
The C/C++ syntax is not very good in my opinion; it requires way too many unnecessary keystrokes and has some really strange stuff assigned to certain operations. (for example, why the heck does = mean "set value to" and == mean "determine equality"?)The problem is that just about every language uses that same syntax, or a very slight variation of it. :p
Its like that to avoid overloading the = operator. You could go ahead and overload it, but you set up an ambiguous situation as shown in the following psuedocode:
x = 1
y = 2
if x = y:
doSomethingWhenTrue(x)
else
doSomethingWhenFalse(x)
What is the value of X? Am I testing the value of X in the if, or am I testing th exit value of an assignment operation? In the first case, I'll execute the false code. In the latter case, i'll execute the true code.
Clarity is a good thing, especially when it only costs one keystroke.
-
Actually, that part is quite good and as you said, will allow for greater precision (always good ;)); I meant that the actual symbols should be something different. (= should be for testing equality, with >> or := for setting a value, as done in discrete math) The usage of semicolons also seems to be somewhat erratic; they appear after most commands to denote the end of the command, but they don't seem to be used at the ends of functions for some reason. It would also be nice if the braces were replaced with standard brackets, since the braces require a shifted keystroke.
-
standard square brackets "[" & "]" are used for array offsets
int array[MAX_ARRAY_SIZE]
array[0] = 256;
array[1] = 512;
...etc...
and if they were used for something else it could cause some abiguities...
-
Originally posted by PhReAk
only if the struct/class/union is a pointer, otherwise its a dot
I wasn't far off. :)
I've never actually written or learned any C/C++ in my life, so I was basically just going from what I've seen posted on newsgroups for conversions between C/C++ and Delphi.
-
Originally posted by CP5670
Actually, that part is quite good and as you said, will allow for greater precision (always good ;)); I meant that the actual symbols should be something different. (= should be for testing equality, with >> or := for setting a value, as done in discrete math) The usage of semicolons also seems to be somewhat erratic; they appear after most commands to denote the end of the command, but they don't seem to be used at the ends of functions for some reason. It would also be nice if the braces were replaced with standard brackets, since the braces require a shifted keystroke.
== can be thought of as a back construct. Consider the cases of greater-than-or-equal or less-than-or-equal or not-equal, and 'equal' makes more sense:
== (equal)
<= (less than or equal)
>= (greater than or equal)
!= (not equal)
Further, == is easier to type than := or >>. >> is already a bitshifting operator in C and overloaded to a stream operator in C++ (programmers correct me if I'm wrong here).
-
Originally posted by mikhael
Further, == is easier to type than := or >>. >> is already a bitshifting operator in C and overloaded to a stream operator in C++ (programmers correct me if I'm wrong here).
Well, I don't know about you, but I consider the "=" sign to be a testing operator or a statement operator - not an "active" operator like "+" or "/".
Of course, that could stem from the fact that I'm a freakish math major (just starting out). My first CSC course was in Pascal which used "=" as a boolean testing operator and ":=" as an assignment operator (which seemed quite natural to me.)
I never made the mistake of putting "=" when I meant ":=" - but I can't count the number of times in my Java course that I've had to fix bugs of If statements that return true 'cause I put
if (x = y)
{
//insert code here
} //end if
-
Of course, that could stem from the fact that I'm a freakish math major (just starting out).
w00t; another math guy! :D What specific stuff are you into? (I'm a calculus nut, especially with the variational and fractional branches ;) :D)
I also tend to think of the equal sign as something that shows that two quantities are equal rather than setting a quantity. In math, equality is denoted by = while definition is shown using a ยบ. This isn't part of the standard character set though, so something else must be used for that (as TurboNed said, Pascal uses :=), but not the normal equal sign. ;)
-
Originally posted by CP5670
w00t; another math guy! :D What specific stuff are you into? (I'm a calculus nut, especially with the variational and fractional branches ;) :D)
Like I said - I'm just starting out. First year Math Major - not really sure what branch I'm going to go into yet.
Okay, okay - let's get this back on topic. (-:
--TurboNed