Recently, we have been working on a project using VC6. Some of our brothers have reflected that they do not quite understand the differences between pointers and references. Currently, developers are familiar with Java, C #, and other languages, which is farther and farther away from the underlying operating system, so that most people have forgotten some things in C/C ++.
Speaking of pointers, anyone who has learned C language is no stranger. This is the essence of C language. Pointer operations are not only efficient, but also very close to the underlying system and easy to control. Therefore, if you are using c/c ++ coding, it is recommended that you first focus on understanding the pointer. Of course, things always have two sides, and there are advantages and disadvantages. The most criticized for pointer operations is its insecurity. Unless you have a thorough understanding of it, there will inevitably be access out-of-bounds, memory leaks, and unpredictable results.
"Reference" is a concept introduced in C ++. We often see that "reference" is used in function calling. In C ++, when calling a function, you can pass parameters in three ways: Passing values, transmitting addresses, and transferring references. For example, the following VC code:
// Main program code
CString strUserId = "001 ";
CString strUserName = "unknown ";
BOOL bFind = FindUserName (strUserId, strUserName );
If (bFind)
{
MessageBox ("User Name:" + strUserName );
}
Else
{
MessageBox ("No found! ");
} // Query user functions
BOOL FindUserName (CString & strUserIdParam, CString & strUserNameParam)
{
If (strUserIdParam = "001 ")
{
StrUserNameParam = "Wang zebin ";
Return TRUE;
}
Return FALSE;
}
Two referenced parameters are used in the FindUserName function. The final running result of the main program code is: a dialog box with the content "User Name: Wang zebin ". The "CString &" in the function indicates that the parameter uses the "transfer reference method ". StrUserNameParam in FindUserName is the same object as strUserName in the main program code. Therefore, any modification to either of them will be reflected in another object. StrUserNameParam is just the alias of strUserName, which is very similar to the file shortcut in windows or the symbolic link of files in linux. If you change the search User Function:
// Search for user functions
BOOL FindUserName (CString strUserIdParam, CString strUserNameParam)
{
If (strUserIdParam = "001 ")
{
StrUserNameParam = "Wang zebin ";
Return TRUE;
}
Return FALSE;
}
The FindUserName function uses the value passing parameter. The final running result of the main program code is: a dialog box with the content "User Name: Unknown ". The "CString" in the function indicates that the parameter uses the "value transfer method ". StrUserNameParam in FindUserName is totally different from strUserName in the main program code. Therefore, any modification to either of them will not be reflected in another object. The content of strUserNameParam copies the content of strUserName. Therefore, strUserNameParam is also called a copy of strUserName. This copy operation is executed when the parameter pressure stack is applied during function calling. If you change the search User Function:
// Search for user functions
BOOL FindUserName (CString * pstrUserIdParam, CString * pstrUserNameParam)
{
If (* pstrUserIdParam = "001 ")
{
* PstrUserNameParam = "Wang zebin ";
Return TRUE;
}
Return FALSE;
}
In this case, the main program code needs to slightly modify the calling function:
BOOL bFind = FindUserName (& strUserId, & strUserName); the address passing parameter is used in the FindUserName function. The final running result of the main program code is: a dialog box with the content "User Name: Wang zebin ". The "CString *" in the function indicates that the parameter uses the "address transfer method ". PstrUserNameParam in FindUserName and pstrUserName in the main program code are two completely unrelated pointer variables, but the content in these two pointer variables is exactly the same and all point to the address of the same memory space, therefore, the effect of modifying any pointer variable pointing to the memory space is the same. The content of pstrUserNameParam copies the content of pstrUserName. pstrUserNameParam is a copy of pstrUserName. This copy operation is executed when the parameter pressure stack is applied during function calling. Through the above example, we can see that when the reference is passed as a function parameter, it has the pointer effect, but it can be used like passing a value. The method is simpler than the pointer. When used as a parameter, the pointer and reference can both be used in the function body to directly change the content stored in the address space they direct, the compiler does not need to create a temporary variable for them to copy and save a copy of their values. The usage of pointers and references in C/C ++ is slightly different:
1. the pointer value can be NULL, but the reference cannot be NULL. If you directly define the variable CString & strVar1; or CString & strVar1 = NULL in VC, this will not work, the compiler cannot scan the syntax. The compiler requires that the initial value be specified when defining referenced variables.
2. Once the reference is initialized, the referenced link pointer cannot be changed, and the referenced object can be changed at any time ).
In the following example, k is initialized as a reference of I. Statement k = j cannot be changed to a reference of j, but the value of k is changed to 6. Since k is an I reference, the I value is also changed to 6.
Int I = 5;
Int j = 6;
Int & k = I;
K = j; // The values of k and I are changed to 6;
Therefore, we come to the conclusion that reference is actually just a syntax concept in a programming language, and there is no difference between the actual code executed after compilation and the pointer. The introduction of this concept brings two benefits:
1. The code is concise and easy to use. The above example can be compared.
2. partially eliminate the insecure usage of pointers. Because it is required to initialize it at the syntax level, there is no risk of NULL pointer operations. One advantage of Java and C # is to remove the pointer concept and eliminate the security of C/C ++ at the syntax level, this is very beneficial for building robust large-scale applications. They use two methods to pass function call parameters: simple type parameters and object type parameters. Take Java as an example:
Class ObjUser {
String strName = "init value ";
Public String toString (){
Return strName;
}
} Public class Test {
ObjUser User = new ObjUser ();
Int Age = 60;
Public void changeObj (ObjUser user ){
User. strName = "changed value ";
}
Public void changeInt (int age ){
Age = 80;
}
Public static void main (String [] args)
{
Test objUser = new ObjUser (); System. out. println ("========================== Print User ========================= ");
System. out. println ("Before call changeObj () method:" + objUser. User );
ORef. changeObj (objUser. User );
System. out. println ("After call changeObj () method:" + objUser. User );
System. out. println ("=========================== Print Age ========================== ");
System. out. println ("Before call changeAge () method:" + Age );
ORef. changePri (objUser. Age );
System. out. println ("After call changeAge () method:" + Age );
}
} Running result:
======================== Print User ============================
Before call changeObj () method: init value
After call changeObj () method: changed value
========================= Print Age ============================
Before call changeAge () method: 60
After call changeAge () method: 60 in Java, if you want to implement the object Value passing method, you can use the clone () method to first copy the object and then call the function.
The C # scenario is basically similar. php also retains the two methods of passing values and transferring references, which are similar to the C ++ syntax.
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/wanghao72214/archive/2009/07/24/4376158.aspx
This article is from the "website architecture home" blog, please be sure to keep this source http://wangzebin.blog.51cto.com/653300/182452