Skip to main content
gamedev.net
🔒 Locked

stupid pointers

Started by duckbob Jan 14, 2002 at 7:56 PM 11 replies 1.1k views
Original Post
duckbob
duckbob
ok, i have this peice of code here, what i want to do is be able to change the values of x, y, z from within the function. I know i can do this with points but i can't get it working. Could someone show me what i need to do? And i know i can make the routine much simpler but i have it this way because i need to modify xyz based on each step of the conditional statements. duckbob *code*
  
bool colideroom(float x, float y, float z)
{
for (int i = 0; i <= NumBoxes - 1; i++)
{
 if (room[i].Px >= x)
 {
  if(room[i].Nx <= x)
  {
   if (room[i].Pz >= y)
   {
    if(room[i].Nz <= y)
    {
     if (room[i].Py >= z)
     {
      if (room[i].Ny <= z)
      {
       return true;
      }
     }
    }
   }
  }
 }
}
return false;
}
  
[edit: added source tags] Edited by - Magmai Kai Holmlor on January 15, 2002 1:13:46 AM
Jeff D
Jeff D
try this:

bool colideroom(float &x, float &y, float &z)

keep the rest the same. I hope that works im still somewhat a newbie.

Jeff D



Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
duckbob
duckbob
that worked, thanks
Shannon Barber
Shannon Barber
Those are called references if you didn''t alraeyd know that. They work like pointers do, but use the non-pointer syntax (and can''t point to null).
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
BeerNutts
BeerNutts
Pointers aren''t stupid, it''s the people that use that that are stupid (not talking about Duckbob, just making a statement
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
duckbob
duckbob
i just have trouble with the concept, i understand that they are useful, and make things possible, but the first time i really learned programing (able to make somthing that did somthing useful) was in visual basic, which you don''t have to think about how it can be coded but only how it can be done

duckbob

gregs
gregs
As I always teach - sarcastically - "Pointers are your friends!"
BSXrider
BSXrider
Not criticising your coding style, but maybe you're not aware of the && operator?

    
bool colideroom(float x, float y, float z)
{
for (int i = 0; i <= NumBoxes - 1; i++)
{
if ((room[i].Px >= x)&&(room[i].Nx <= x)&&(room[i].Pz >= y)
&&(room[i].Nz <= y)&&(room[i].Py >= z)&&(room[i].Ny <= z))
{
return true;
}
}

return false;
}


- seb



Edited by - bsxrider on January 15, 2002 7:26:01 PM
duckbob
duckbob
did ya read my post, i have it like that for a reason, i know i can use the && operator, but once i got my problem fixed, which i did, i put operations between each step which is dependant on each step...progressivly, so in this instance i didn''t need to condence that in this instance

BSXrider
BSXrider
Whoops sorry, I tend to just scan read a lot of the time

- seb
Mark Duffill
Mark Duffill
Alternately to stop lots of indented if's
    
bool colideroom(float x, float y, float z)
{
for (int i = 0; i <= NumBoxes - 1; i++)
{
if (room[i].Px < x) ) continue;
if (room[i].Nx > x) ) continue;
if (room[i].Pz < y) ) continue;
if (room[i].Nz > y) ) continue;
if (room[i].Py < z) ) continue;
if (room[i].Ny > z) ) continue;

return true;
}

return false;
}


Just personal taste I suppose

Edited by - Mark Duffill on January 17, 2002 8:34:46 AM
Mullen
Mullen
And if you''re strictly proggin in C and don''t want to use references...
(This is just an example, not your code)
  

void squaren(int* num)
{
int n = *num;
n = (n*n);
*num = n; // set the value pointed by num to n''s value

}

//

squaren(#); // pass the address-of num

//

" When you know what is; you''ll know what isn''t. "~Miyamoto Musashi
merlin9x9
merlin9x9
When you have a function that can or will modify its arguments, use pointers rather than references. In other words...
Use this:
    
void blah(int* a, int* b)
{
*a = 10;
*b = 12;
}

...instead of:

void blah(int& a, int& b)
{
a = 10;
b = 12;
}


Here's why. If you're calling the reference version of the function, the call will look something like this: blah(var1, var2) . Just from looking at that line of code, can you tell whether or not those arguments are going to be modified? No. For all you know, they might just be passed by value. To know better, you'd have to have remembered how you implemented blah , and that gets exponentially difficult and impractical as your project grows. Now, if you use pointers in that function, it must be invoked like this: blah(&var1, &var2) . To pass those objects to the function through the pointers, we need to take the objects' addresses. The only reason we'd do that—following our convention—is to modify them. So, the only types of references you should ever use are const references, with very few exceptions. Even Bjarne effectively says this in his book. Even if you choose to ignore Bjarne and me, learn pointers . Otherwise, forget C++ and learn Visual Basic, Pascal, Java, or C#.

Edited by - merlin9x9 on January 17, 2002 12:57:50 PM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.