Comparison between C ++ and C #: function (3) Name and variable name

Source: Internet
Author: User
Document directory
  • Function name duplication
Function overload and function name duplication

 

Function name duplication

We know that in the same scope, two variables with the same name cannot be defined. you cannot define two functions with the same name. however, we can determine whether two functions have the same name not only based on the function name, but also based on the function signature. the function signature is determined by the function name and parameter list. the function name is the same as the function name. however, different parameter lists are complicated.

The numbers of parameters in the table list are naturally different. So let's take a look at the situations where the number of parameters is the same.

 

In C #, since the form parameter can only use the ref and out modifiers, it is relatively simple.

Void fun (INT age ){}

Void fun (ref int age ){}

These are two different functions.

Void fun (Out int age) {} and void fun (ref int age) use the same function signature.

Void fun (INT age) {} is the same function signature as int fun (INT age) {return 1}, because the return value type is not in the scope of function signature considerations.

 

C ++ is a little more complicated.

(1) void fun (INT [5]) {} and void fun (INT [2]) {}

This is the same function signature, because when an array is used as a parameter, it only transmits the first address of the array. The size of the specified array will be ignored. No matter how large the specified size is, it will be useless.

So if void fun (INT [5]) {} is defined

Int arr1 [] = {1, 2, 3 };

Int arr2 [] = {7, 6, 5, 4, 3 };

Fun (arr1 );

Fun (arr2); // both calls are correct

In C #, the void fun (INT [5]) {} function cannot be compiled. The array size cannot be specified.

 

If you want to upload an array and specify the array size, you should use the array reference.

Void fun (INT (& ARR) [5]) {} at this time, the function size is different from the specified size. for example, function void fun (INT (& ARR) [2]) {} is a function with different function signatures.

So if void fun (INT [3]) {} is defined

Int arr1 [] = {1, 2, 3 };

Int arr2 [] = {7, 6, 5, 4, 3 };

Fun (arr1); // correct

Fun (arr2); // Error

 

(2) void fun (int * A) {} is the same function signature as void fun (int A [5 ]) {}

As mentioned above, passing an array as a parameter is actually equivalent to passing in its first address. The type of the first address of the int [] array is int * type.

 

(3) void fun (int * A) {}and void fun (const int * A) {} are two different function signatures.

Void fun (Int & A) {}and void fun (const Int & A) {} are two different function signatures.

They are different if they are added without Const. But this is only for pointers and references and is not applicable to any other types.

For example, void fun (int A) {} and void fun (const int A) {} are the same function signatures.

This looks a bit strange. why other types will not work. it is said that this design is intended to be compatible with C. In C, parameters without const are treated as the same. then there are pointers in C. Why is it different to add a pointer without adding const. why?

 

When you understand the function name, it is easy to understand what is called function overload. That is, several functions with the same name and different function signatures are defined.

For example, the following three functions are defined:

Void fun (INT age, string name ){}

Void fun (int * Age ){}

Void fun (const Int & age ){}

This is called function overload.

 

Function Name and variable name

The above is to define different functions as long as the function signature is different. But does the function name conflict with the common variable name? Certainly.

For example, in a class

Class Arwen

{

Public:

Int fun;

Void fun (); // error. The function name fun is the same as the member variable name

}

However, if one is a global scope and the other is a local scope, there will be some differences, such

Class Arwen

{

Public:

Int fun ();

Void test () {int fun ;}

// No error will be reported if you define a variable fun in function test. however, in the test function, the local variable fun shields the function name fun, which is the same as the rule for shielding global variables in general variables. therefore, if function test calls function fun, an error occurs. For example:

// Test ()

{

Int fun;

Fun (); // Error

}

 

If the function name is the same but the parameters are different, no error will be reported, and if the variable name and function name are the same, an error will be reported?

Why can't we make it easier to get the same name between a function and a variable. because function names can also be used in the same way as common variable types, for example, if there is a function pointer, we can assign the function name as the right value to the function pointer. it is difficult for the compiler to determine whether two identical names are common variables or function names. so the compiler just won't let you have the same name. in fact, if the compiler needs to do more, it can make the function name the same as the common variable name. it is said that some languages or compilers can indeed make the function name the same as the variable name. there is no practical benefit for having the same name between a function and a variable, so it is still less effort-consuming. in addition, the compiler allows the function name to be the same, as long as the function signature is different, it is natural to support function overloading. this is a tough but thankless task. in fact, many errors in the compilation language do not mean that they really cannot be the same, but the compiler will follow certain rules to check. if you change the compiler, the rules may be different. at this time, the error is also true.

 

In C #, functions and variables with the same name are handled in the same way as in C ++.

 

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.