do i have a use for pointers?

Started by
38 comments, last by ToohrVyk 18 years, 3 months ago
It's also very dangerous and should land pretty squarely in UB-land, because you are effectively reseating a reference.

Store pointers as data members, as a rule of thumb. Unless it shouldn't change over the lifetime of the object, and you can set it up in the initializer list of each constructor.
Advertisement
Quote:Original post by T2k
nah, your code shows a valid use for pointers (where they produce more readable code...) and my version in the source tags also uses a nonstandard extension and breaks the automatic generation of the assignment generator...


Actually, he tried to point out a case where pointers can be used, but references can't. And here is another short example where no pointers can be replaced by references:

void Create( int* & x ) {  if( x != NULL ) { delete x; }  x = new int( 18 );}void Display( int* x ) {  if( x != NULL ) { cout << (*x) << endl; }}int main() {  int* x = NULL;  Display(x);    x = new int(42);  Display(x);  Create(x);  Display(x);  delete x;  return 0;}
Quote:Original post by Zahlman
It's also very dangerous and should land pretty squarely in UB-land, because you are effectively reseating a reference.


No, he isn't. He is taking the address of a reference (which returns a temporary lvalue equal to that address), casting it and changing it to something else. Happiness and cuddling ensues when the temporary disappears and nothing has changed.

EDIT: well, except the fact that he is assigning a new value to a temporary object, which is not a lot better.
Quote:Original post by ToohrVyk
Quote:Original post by Zahlman
It's also very dangerous and should land pretty squarely in UB-land, because you are effectively reseating a reference.


No, he isn't. He is taking the address of a reference (which returns a temporary lvalue equal to that address), casting it and changing it to something else. Happiness and cuddling ensues when the temporary disappears and nothing has changed.

EDIT: well, except the fact that he is assigning a new value to a temporary object, which is not a lot better.


Actually, it does reset the reference, but it's just an awful hack. "&leader" seems to return a pointer variable of type "ArmadaShip*" , which I suppose is const. By using reinterpret_cast he manages to write to it. It works kinda like this situation:

void main(){  int a=1;  int b=2;  int* const ptr=&a;  //ptr=&b; ERROR C3892: 'ptr' : you cannot assign to a variable that is const  reinterpret_cast<int*>(ptr)=&b;//Hackity hack, but it works  printf("Number= %d\n",*ptr);//prints 2  return;}
No doubt I'll quickly be put in my place if I get this wrong, but I don't use pointers when I want to get a new value, based on an old one.

However, if I have only one 'object' (say for example a back buffer to put my graphics on,before I send them to the screen), I'll use a pointer, as I don't want to keep grabbing (1280*1024) plus bytes of memory every 60th of a second. Better to just get the one and reuse it.
Quote:Original post by mikeman
Actually, it does reset the reference, but it's just an awful hack. "&leader" seems to return a pointer variable of type "ArmadaShip*" , which I suppose is const. By using reinterpret_cast he manages to write to it. It works kinda like this situation:

*** Source Snippet Removed ***


I'm afraid your code does something else entirely. You are reseating a const pointer, which is not a very hard thing to do (and besides, const_cast was made for this). And as I mentioned earlier, by modifying the value of (&a), you are only changing the value of a temporary variable that is initially equal to the address of the referenced object. You make no changes (permanent or otherwise) to the actual reference.

If you want to try and reseat a reference, here is a short snippet of code that will allow you to test if you actually managed to reseat it:

int a = 10;int b = 20;int & c = a;assert( &c == &a );// [insert code to reseat the reference]b = 15;c = 16;assert( c == b );assert( &c == &b );
Quote:
And as I mentioned earlier, by modifying the value of (&a), you are only changing the value of a temporary variable that is initially equal to the address of the referenced object. You make no changes (permanent or otherwise) to the actual reference.


Then why does this piece of code prints "2" instead of "1"?

void main(){int a=1;int b=2;int &c=a;reinterpret_cast<int*>(&c)=&b;printf("%d\n",c);return;}



You're right. I was confused by the language extensions.
Quote:Original post by ToohrVyk
You're right. I was confused by the language extensions.


its an argument for using pointers in that situation. references are very good, and preferable in many cases, but they are not equivilent to pointers.

you shouldnt abuse the language just to get the reference to act like a pointer when you can just use a pointer...
Quote:Original post by rip-off
its an argument for using pointers in that situation. references are very good, and preferable in many cases, but they are not equivilent to pointers.

you shouldnt abuse the language just to get the reference to act like a pointer when you can just use a pointer...


+1

This topic is closed to new replies.

Advertisement