0

I was reading Hacker News and this article came up. It contains a raytracer that the code is written on the back of a business card. I decided it would be a good academic challenge to translate the c++ to python, but there's a few concepts I'm stuck on.

First, this function comes up: i T(v o,v d,f& t,v& n){...} Which is translated to int Tracer(vector o, vector d, float& t, vector& n){...} What does the float& mean? I know that in other places & is used as a == is that the case here? Can you do that in c++?

Second, I noticed these three lines:

for(i k=19;k--;)  //For each columns of objects
for(i j=9;j--;)   //For each line on that columns
if(G[j]&1<<k){

I know the << is a the bit shift, and I assume the & is ==. Are the for loops just like one for loop in an other?

Finally, this line: v p(13,13,13); I am not quite sure what it does. Does it create a class labeled by p that extends v (vector) with the defaults of 13,13,13?

These are probably dumb questions, but I want to see if I can understand this and my searching didn't come up with anything. Thank you in advance!

Hovestar
  • 1,544
  • 4
  • 18
  • 26
  • No, & is not ==. Look on any operator precedence chart and you'll see the difference. – chris Sep 24 '13 at 16:18
  • 1
    the ampersand in C++ is commonly referred to as the "address of" operator. Basically, it means those parameters are passed by reference. – Duncan Sep 24 '13 at 16:22
  • 3
    `&` is for "reference" or "bitwise and" – Jarod42 Sep 24 '13 at 16:23
  • @chris in a few places the code looks like this, `if(m&1){ //m == 1` with the commenting I assumed that the & is used to evaluate that m is one, but it might be at m is one so it acts as true to c++ – Hovestar Sep 24 '13 at 16:26
  • @Hovestar, Yes, and when `m` is 3, it also evaluates to true. Bitwise AND is not the same as equality. It's the same in C. – chris Sep 24 '13 at 16:30
  • Okay I was just reading as that, my bad thanks @chris – Hovestar Sep 24 '13 at 16:30

2 Answers2

3

What does the float& mean?

Here, & means "reference", so the argument is passed by reference.

I know that in other places & is used as a == is that the case here?

& means various things in various contexts, but it never means ==. In this case, it's not an operator either; it's part of a type specification, meaning that it's a reference type.

I know the << is a the bit shift, and I assume the & is ==

No, it's a bitwise and operator. The result has its bits set where a bit is set in both operands. Here, with 1<<k as one operand, the result is the kth bit of G[j]; so this tests whether that bit is set.

Are the for loops just like one for loop in an other?

Yes. If you don't use braces around a for-loop's body, then the body is a single statement. So in this case, the body of the first loop is the second loop. To make this clear, I would recommend indenting the body of the loop, and using braces whether or not they are strictly necessary. But of course, I don't write (deliberately) obfuscated code.

Finally, this line: v p(13,13,13);

v is a class with a constructor taking three arguments. This declares an variable called p, of type v, initialised using that constructor; i.e. the three co-ordinates are initialised to 13.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Thank you very much, but on the last one I knew that it created an object of type v, but what does it do with that? and what does the p() do? It has the type declared as v, but then it says P() and p isn't said anywhere else in the code before that. – Hovestar Sep 24 '13 at 20:23
  • 1
    @Hovestar: it declares and initialises a variable called p, with type v. – Mike Seymour Sep 24 '13 at 21:36
1

When you seeVector& n it is referencing the vector passed into the function. This means that you can change n inside of this function without having to copy it to another Vector or without returning the Vector. This previous answer should be helpful to you.

Community
  • 1
  • 1
Douglas Tober
  • 300
  • 4
  • 15
  • So it's pretty much the same as a global variable? Sorry I don't code c++ and I only have limited experience with ANSI C. Thank you very much. – Hovestar Sep 24 '13 at 16:29
  • 1
    Nutomic has it correct, this is similar to a pointer. The reason that references are much more powerful than a global variable is that a global can be accessed from any classes inside your project and a reference allows you to keep your variables encapsulated (hidden from end users). – Douglas Tober Sep 24 '13 at 18:50