C++ Question
BlitzMax Forums/BlitzMax Programming/C++ Question
| ||
Sorry to ask this here, but there isn't really any "other languages" board here. Well, the thing is, I'm adapting a C++ code to BlitzMAX. I don't know much of C++, so I'm having some doubts.struct ArbiterKey { ArbiterKey(Body* b1, Body* b2) { if (b1 < b2) { body1 = b1; body2 = b2; } else { body1 = b2; body2 = b1; } } Body* body1; Body* body2; }; Body is a struct defined in another header. I can't find a custom < operator defined anywhere in the code, so I'm wondering how exactly the "b1 < b2" part works. What exactly is it comparing? The variable adresses? Thanks in advance. |
| ||
A quote: "Comparison - NO The comparison operators do not work on structs. To compare structs, compare individual fields. if (p1.x==p2.x && p1.y==p2.y) . . .It is not possible to write p1==p2. There are good reasons to forbid comparison. What would a greater than comparison even mean on a Point for example. A bit-by-bit equal comparison is not feasible in general because there may be padding or unfilled elements (eg in a C-string). " It's from this site: http://www.fredosaurus.com/notes-cpp/structs/struct-ops.html Another useful read on structs: http://en.wikipedia.org/wiki/C++_structures_and_classes On another forum, I found a hint that this -could- have something to do with the priority of the structs, meaning "the order in which they come out of the heap": http://www.thescripts.com/forum/thread628342.html Geez, no wonder Niklaus Wirth said that "C++ is an insult to the human brain...." ;-) |
| ||
Love the quote :P I was told that this is comparing the address of the variables. If that's really the case, would this work? ArbiterKey(b1:TBody, b2:TBody) if varptr b1 < varptr b2 then body1 = b1; body2 = b2 else body1 = b2; body2 = b1 end if |
| ||
what is the function suppose to do? And are you sure that header file didn't have an accompanying cpp file |
| ||
If that's really the case, would this work? Nope. You would be as well implementing your own Compare() method, in which you do whatever calculations required to determine whether one is less than or equal to the other. |
| ||
And how exactly can I do that? And in that C++ code, what exactly is being compared? |
| ||
b1, b2, body1, body2 are all pointers. The comparison is between pointers, i.e. addresses. Then body1 is assigned the lower address and body2 the higher. Now you have to figure out why this is being done. |
| ||
So, will this work with a TBody object or not?if varptr b1 < varptr b2 then body1 = b1; body2 = b2 else body1 = b2; body2 = b1 end if If not, how do I do that in blitz? |
| ||
It's hard to say what is appropriate here. There are too many differences in the way things are done in C++ and BlitzMax. For example, a C++ struct and a BlitzMax Type are essentially the same thing. But when you do a 'new struct_thingy' in C++ the resulting address is the same as the address of the first member data within the struct. This is not true in BlitzMax. The address of the New object is not the same as the address of the first Field. As a result I think a "line by line" translation of C++ to BlitzMax is doomed. You really need to work out what the C++ code accomplishes and write BlitzMax code to produce the same result. |
| ||
But still, if that is indeed comparing the adresses of the objects, then varptr will work, won't it? |
| ||
Perhaps But using .compare is the correct way of doing it on an object |
| ||
How does that compare work? What does it do, and how do I use it? |