Using pointers or applications as parameters to pass by application can reduce system overhead. Because if a parameter is a large data object and is directly used as a parameterCopyOne copy is used as a function parameter, which consumes more system overhead than transmitting by application. There is nothing to say about passing the function value.ProgramPersonnel, everyone must be clear, just record it.
Write your ownDemoCodeAs follows:
// C ++ basetest. cpp: defines the entry point of the console application.
//
# Include " Stdafx. h "
# Include < Windows. h >
# Include < Iostream >
Using Namespace STD;
Void Func_one ( Int * );
Void Func_two ( Int );
Void Func_three ( Const Int * );
Void Func_four ( Const Int );
Int _ Tmain ( Int Argc, _ tchar * Argv [])
{
Int A = 8 ;
Int * A_ptr = & A;
Const Int * A_const_ptr = & A;
Const Int A_const = 9 ;
// Func_two ();
// Func_two (a_const );
// Func_one (a_ptr );
// Func_one (a_const_ptr ); // Error: const int * cannot be converted to int *
// Func_three (a_ptr );
// Func_three (a_const_ptr );
Func_four ();
Func_four (a_const );
While ( True )
{
}
Return 0 ;
}
Void Func_one ( Int * A)
{
Cout<"Func_one"<Endl;
}
Void Func_two ( Int A)
{
Cout<"Func_two"<Endl;
}
Void Func_three ( Const Int * A)
{
Cout<"Func_three"<Endl;
}
Void Func_four ( Const Int A)
{
Cout<"Func_four"<Endl;
}
The demo above is only executed Func_one When the form parameter is an integer pointer and the real parameter is a pointer defined by const, an error is reported. Why? In my opinion Func_one In a function, the form parameter is an integer pointer. That is to say, it is valid to modify the data pointed to by the passed real parameter in the function body, but the real parameter is a constant pointer defined by const, it does not allow you to modify the pointer and the content pointed to by the pointer. To avoid conflicts, the compiler will think this is wrong. Why? In func_two, whether the real parameter is defined or not, const can pass normally? This is very simple. The real parameter passed to the function is just copied to it, and the function does not change the value of the real parameter, so this is allowed. Func_three Although the form parameter is represented as a pointer, it is defined by const, which means that if the function makes any modification to the real parameter, It is not syntactic. Func_four Similarly.
In fact, you only need to understand that any constant defined by const cannot be changed under any circumstances.