The function overload problem that the C ++ center is involved in the const parameter.

Source: Internet
Author: User

Record Lookup (phone );

Record Lookup (const phone );

Are these two function overloading?

This also needs to find the answer from the relationship between the real participation parameters, the difference is only whether to define the form parameters as const, this difference does not affect the objects passed to the function, therefore, the second function declaration is considered as the first repeated declaration.

Cause: the real parameter passing method. When copying a copy of a real parameter to a shape parameter, it does not consider whether the shape parameter is a const, because the function operates only a copy. The function cannot modify the real parameter. Therefore, you can pass a const object to a const parameter or a non-const parameter. There is no essential difference between the two parameters.

For example:

Record Lookup (15001805405); // assume that you want to pass your mobile phone number.

Which of the above functions should be called at this time? Obviously, both parameters can be used. Both parameters and real parameters have their own parents. They have their own space and do not interfere with each other.

However, at this time, the compiler will be dumpfounded, And the compiler will become Sb due to the ambiguity. Therefore, the conclusion is that this set of functions are not part of function overloading.

PS: the equivalence of the const parameter is only applicable to non-reference parameters. Functions with const reference parameters are different from those with non-const reference parameters. Similarly, if a function has a pointer to the const type, it is not the same as a function with a pointer to a non-const object of the same type.

 

Record Lookup (const phone &);

Record Lookup (Phone &);

What about the two? Is it a function overload?

The answer is yes. Yes.

For example:

Const phone A = 15001805405; // second contribution

Record Lookup (a); Which one should I call?

Obviously everyone knows the one above. I don't need to worry about the reason.

Someone will ask this question:

Phone a = 15001805405;

So what should I call this?

Which of the following? The one above?

Conclusion: In this case, the non-const can be matched first. If not, the const can be matched.

Is there a basis? Yes. The test results show that:

The test is as follows:

 

# Include <iostream>
Using namespace STD;
Int Sb (const Int & B)
{
Cout <"this evening I made her dislike! This is my fault! "<Endl;
Return B;
}
Int Sb (Int & B)
{
Cout <"but I think it's the first time. Forgive me. It's hard and tired to be a human! "<Endl;
Return B;
}
Int main ()
{
Int A = 2;
Cout <Sb (a) <Endl;

Cout <"something you want to understand is happiness, or pain! "<Endl;
Return exit_success;
}

Let's annotate one of them to compile and check the result, and then remove the comment to see who matches the result first when the two coexist!

In a bad mood, I always want to complain to Mo mo, and I feel a lot of grievance and anger. I really like her. Friends are precious and should be cherished!

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.