stupid pointers

Started by
11 comments, last by duckbob 22 years, 3 months ago
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
Advertisement
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(&num); // pass the address-of num//  
" When you know what is; you''ll know what isn''t. "~Miyamoto Musashi
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

This topic is closed to new replies.

Advertisement