C ++ function Overloading

Source: Internet
Author: User
// Strtref. cpp -- using structure references
# Include <iostream>
Using namespace STD;

Void fun (const int * num)
{
// Num = 435;
Cout <* num <", const fun" <Endl;
}

Void fun (int * num)
{
// Num = 435;
Cout <* num <", not const fun" <Endl;
}
Int main ()
{
Const int num = 999;

Const int * P = & num;

Int AVG = 123;

Int * P2 = & AVG;

// Because the fun parameter is pointer type, it can be reloaded according to const and non-Const. If void fun (INT num) and void fun (const int num)It cannot be reloaded.
Fun (p); // output "999, const fun"
Fun (P2); // output "123, not const fun"
Return 0;

}

 

However, note that non-const real parameters can also match const parameters, but const real parameters cannot match non-const parameters:
1. Non-const real parameters can also match const Parameters

# Include <iostream>
Using namespace STD;

Void fun (const int * num)
{
// Num = 435;
Cout <* num <", const fun" <Endl;
}

Int main ()
{
Int AVG = 123;
Int * P2 = & AVG;

Fun (P2); // output "123, const fun"
Return 0;
}

2. Const real parameters cannot match non-const parameters. The following sectionCodeIf the compilation fails, an error is reported, indicating that the const int * cannot be converted to int *"
# Include <iostream>
Using namespace STD;

Void fun (int * num)
{
// Num = 435;
Cout <* num <", const fun" <Endl;
}

Int main ()
{
Int AVG = 123;
Const int * P2 = & AVG;

Fun (P2 );
Return 0;
}

 

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.