[Original] const real parameters and const constant parameters

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.