Monday, July 27, 2009

Pointer to reference conversion?

in c++ how do I convert a pointer to a reference without using rtti depedency, I know its dangerous but the conversion is only made after a null pointer check has been made

Pointer to reference conversion?
Can't you just de-reference the pointer?





e.g.





#include %26lt;iostream%26gt;





int main(char **argv, int argc) {





// declare a variable


int number = 123;





// declare a pointer and initialise


// it to point at the number


int *ptr = %26amp;number;





// declare a reference, and


// initialise it to the number


// by dereferencing the pointer


int %26amp;ref = *ptr;





std::cout %26lt;%26lt; "The number is: " %26lt;%26lt; ref %26lt;%26lt; std::endl;





return 0;





}


No comments:

Post a Comment