Whats this strange code...c++

Started by
24 comments, last by me22 18 years, 4 months ago
Quote:Original post by Anonymous Poster

Quote:Original post by dragongame
But try to use NULL or 0L because pointer are of type long int which is different than a normal int on (some) 64-Bit Machines.
At least Stroustrup recommends using 0, and I'd rather believe him than you :)
http://www.research.att.com/~bs/bs_faq2.html#null


Ok, I'll use 0 then.
Advertisement
Another thing that you can do this with initialization list:
// ... Header ...class Example {    int test;public:    Example(int test);};// ... Implementation file ...Example::Example(int test)  : test(test) { }


This will assign the value of the test argument given to the constructor to the test property of the object.
Quote:Original post by Roboguy
Another thing that you can do this with initialization list:
// ... Header ...class Example {    int test;public:    Example(int test);};// ... Implementation file ...Example::Example(int test)  : test(test) { }


This will assign the value of the test argument given to the constructor to the test property of the object.


Yeah I worked that out, its pretty cool, instead of having to do the old this->test= test
Hi,

on an Alpha Machine an int is 32-bit and a pointer is 64 bit.
o is of type int. I use 0L because when you know what type a pointer is, you should compare with the same type!
So compiler will like make a 0 to 0L anyway, but why not do it in the first place.
And people often cast pointers to int, which will make a program not run on a 64-bit machine.
“Always programm as if the person who will be maintaining your program is a violent psychopath that knows where you live”
dragongame: Pointers are a unique type, and are not the same as any other type. 0 is a special value and is not the same as any literal number because when you convert 0 to a pointer, it MUST become a null pointer, and two null pointers must compare equal.

Note that null pointer and 0 are not the same thing. A 0 becomes a null pointer when converted to a pointer type, but a null pointer can become any value when you convert it to any non-pointer type.

It is perfectly valid for:
void *Pointer = 0;if(unsigned long(Pointer) == 0){   cout << "Pointer is 0";}else{   cout << "Pointer is not 0";}
to print out "Pointer is not 0" because converting a null pointer to an integer is not required to evaluate to 0. The special conversion of 0 is only guaranteed one way (from 0 to null pointer) and the reason comparing a pointer to 0 works is that the only valid automatic conversion is to convert the 0 to a pointer of the same type (so it becomes a null pointer) and then after the conversion the null pointer is compared with your pointer variable.

Casting a pointer to any non-pointer type is not guaranteed to work no matter what integer type you use, except in C where there is a "intptr_t" type defined so that you can convert a void pointer to intptr_t then back to a void pointer and it will have the same value, but there is still no guarantee about what the value will be, or that if you add 1 to the intptr_t that it would point to the next byte, or anything like that. Converting pointers to integers only works because the major vendors decided it should. Counting on such behavior makes a program non-standard.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Quote:Original post by Grafalgar
Actually explicitly calling the constructor does not create any temporary objects :) Calling the constructor does not create the object, instead the constructor is called right after memory is allocated for an object.

So in this case the base constructor will be called twice : Once when the actual object's memory is allocated and the constructors are called implicitly, and once explicitly by the programmer in the body of the derived class' constructor.


Nope. It's not calling the constructor again, it's creating a temporary object. It's the same as, for example, creating an unnamed functor or iterator for use by an algorithm:
transform( c.begin(), c.end(),           c.begin(),           bind2nd( plus<int>(), 5 ) );// look, an unnamed functor.copy( c.begin(), c.out(),      ostream_iterator<int>( cout, "\n" ) );// look, an unnamed iterator

This topic is closed to new replies.

Advertisement