C++指针&引用

http://www.cplusplus.com/articles/1075fSEw/


A pointer or a Reference (Alias according to some). Does the complier really care? 

Lets do this simple experiment, I think it can help any one trying to understand pointers and references.

lets declare two flavors of some simple function
but lets use only the int& version for now.

1
2
      //void some_function(int* i){cout << "\nvalue is " << *i << endl;};
      void some_function(int& i){cout << "\nvalue is " << i << endl;};


the difference is that one takes a reference to an integer and the other
takes a pointer. So far so simple and good, now lets call the function using 
pointers.

initialize things. this is only so that we need to re cast fp
later, I mean it makes no sense other than for the example here.
so now fp is a pointer to some byte. it does not know he points
to a function anymore.
1
2
      char* fp = reinterpret_cast<char*>(&some_function);
      int ii = 5;


first call to some_function recasting fp
1
2
      
      reinterpret_cast<void(*)(int&)>(fp)(ii);

this will output the value in ii which is 5.

so no problem all simple.

second call to some_function
Now lets call it with a slight change
1
2
  
    reinterpret_cast<void(*)(int*)>(fp)(&ii);


this will out the exact same 5 of course because is calling
the same function. but notice that I cast the pointer using int*
and not int&, and the function declaration we are using is
the int& one. 

In the first call we casted using the correct type and we did not
have to call using &ii. In the second case because we casted using int*
we needed to call using &ii to convert it to a POINTER.

I think this simple example shows you that the compiler does not care much whe it sees a reference and some one calling it with a different signature like
int*, because it by then changed the int& into int* every where so.

In other words as far as the compiler is concerned it only sees
 
void some_function(int* i){cout << "\nvalue is " << *i << endl;};

even if you wrote as 
 
void some_function(int& i){cout << "\nvalue is " << i << endl;};


and the call using the pointer with the reference will be changed into
the call using the pointer.

you can play around commenting the code and calling the function in the two manners and see how they all work.

well I hope this helps or at least entertains some one.


====================================================================================

一)

#include<iostream>


using namespace std;


void some_function(int* i){cout << "\nvalue is " << *i << endl;};


int main()
{
        char* fp = reinterpret_cast<char*>(&some_function);
        int ii = 5;
        reinterpret_cast<void(*)(int &)>(fp)(ii);
        return 0;
}

二)

#include<iostream>


using namespace std;


void some_function(int* i){cout << "\nvalue is " << *i << endl;};


int main()
{
        char* fp = reinterpret_cast<char*>(&some_function);
        int ii = 5;
        reinterpret_cast<void(*)(int *)>(fp)(&ii);
        return 0;
}

三)

#include<iostream>


using namespace std;


void some_function(int& i){cout << "\nvalue is " << i << endl;};


int main()
{
        char* fp = reinterpret_cast<char*>(&some_function);
        int ii = 5;
        reinterpret_cast<void(*)(int &)>(fp)(ii);
        return 0;
}

四)

#include<iostream>


using namespace std;


void some_function(int& i){cout << "\nvalue is " << i << endl;};


int main()
{
        char* fp = reinterpret_cast<char*>(&some_function);
        int ii = 5;
        reinterpret_cast<void(*)(int *)>(fp)(&ii);
        return 0;
}

你可能感兴趣的:(C++指针&引用)