Comparison between C ++ and C #: function (I) C # problems with parameter passing

Source: Internet
Author: User
Document directory
  • But this problem may be a big problem in C.
  • What about Question 2 and C?

Functions can modularize the code and facilitate reuse of the Code. C # inherited from C ++ has similar functions, but there are some minor differences. some types of checks in the entire C # syntax system are stricter than those in C ++, which also reduces potential errors in the code.

A function is composed of four parts: Return Value Type, function name, parameter list, and function body.

We know that each variable has a name, and two variables with the same name cannot be defined in the same scope. the same is true for functions. however, different functions are not distinguished by function names, but by function names and parameter lists. function names and parameter lists are also called function signatures. as for the type of the return value and the function body, you can skip this step.

 

Parameter type

 

The Return Value Type in C ++ and C # Must be explicitly specified. If the return type is null, it must be expressed by void. for example, void function (int A, int B). In C, it can be explicitly specified. for example, function (int A); the default return value is int.

 

We define the parameter list in the function, which is also called the form parameter list. When the function is actually called, the parameter is passed in. The value of the parameter is called the real parameter. when defining a parameter in C ++, you can only specify the type. You can not specify the type name, such as void function (INT, INT). in C #, this is incorrect, the parameter name must be specified. for example, void function (int A, int B );

When a real parameter is passed in, the compiler will judge the type matching. if the type does not match, an error is returned. if the real parameter type can be implicitly converted to the form parameter type. however, the type conversion requirements in C ++ are loose. all numeric types allow mutual conversion.

For example, void fun (unsigned short SH); // it is okay to pass int, float, long, and double values. however, it is obvious that it is prone to problems when other types are passed in. if it is a positive integer int, float and so on may only have different precision. however, if a negative number is passed in, the result is totally incorrect. of course, if a string is input, an error will occur.

In C #, only type conversion does not result in any loss of precision, and negative numbers cannot be transferred when the form parameter is of an unsigned type. for example, void fun (uint num); // at this time, it is okay to pass short or positive Int. if-3 or 3.14 is not supported.

 

C ++ parameter transfer

We often hear about value-based transmission and reference-based transmission. In fact, it is easy to compare parameters with common variables.

For example, a function

Void Arwen (INT age, string & name)

{

Age = 88;

Name = "weiwen ";

}

Int myage = 24;

String myname = "Wen ";

Arwen (myage, myname );

Cout <myage <myname; // at this time, myage is still 24, and myname is weiwen

 

We can equate the above process with the following operations.

// Define two variables first

Int myage = 24;

String myname = "Wen ";

// Then define two variables

{

Int age = myage;

Int & name = myname;

Age = 88;

Name = "weiwen ";

}

This is easy to understand. However, the variable scope in the parameter list is only the whole function body, and the real parameter name and the actual parameter name are the same, it does not affect.

 

C # parameter transfer

 

The above is about C ++. Let's take a look at the value transfer and reference transfer in C # And C. the value transfer is the same as that of C ++. in reference mode, the keyword ref or out is not used.

But in fact, in C #, ref and out can only be passed by reference for value types, but not for reference types, because the reference type is actually quite a reference type, therefore, you can use ref or do not actually achieve the same effect. however, some string types in the reference type are a little special. It is very different if you use ref instead. although the string type is a reference type, C # uses some special processing to make the string use the same effect as the value type.

The above function should be written in this way

Void Arwen (INT age, ref string name)

{

Age = 88;

Name = "weiwen ";

}

Int myage = 24;

String myname = "Wen ";

Arwen (myage, ref myname );

Console. Write (myage );

Console. Write (myname );

// At this time, myage is still 24, and myname is weiwen

The usage of ref and out is almost the same. There is only a small difference. It is necessary to ensure that the value has been initialized when the ref is passed in.

For example, string myname;

Arwen (myage, ref myname); // This will result in an error. myname has not been initialized yet.

The value passed in the function body must be modified and assigned again.

For example, void Arwen (out string name) {return ;}

String myname;

Arwen (Out myname); // error, because the value of myname is not modified in the function body,

If you change the function to void Arwen (out string name) {name = "wei ";}

In this way, Arwen (Out myname) is called correctly.

The ref and out mechanisms are easy to achieve, that is, to ensure that the passed parameters are initialized under any circumstances when the values are passed, either before or after the values are passed.

 

C ++ pointer Parameters

 

For example, a function

Void change (int * IP)

{

* IP = 22;

IP = 0;

}

Int num = 11;

Int * pnum = & num;

Change (pnum );

Cout <* pnum; // The value is 22.

 

We can still regard the above equivalence as the following operation using the method mentioned above.

Int num = 11;

Int * pnum = & num;

{

Int * IP = pnum;

* IP = 22;

IP = 0;

}

 

// Sometimes, to prevent the value indicated by the pointer from being modified in the function body, you can use const to modify the pointer. For example, void change (const int * IP );

 

 

"Pointer Parameters" in C"

C # It is easy to mislead people, that is, there is a so-called value type, reference type. In fact, it is completely different from passing by value, passing by reference.

All basic types in C # And struct are value types, and they are created in the stack. the string, array, and custom class belong to the reference type. although C # does not contain pointers, we can regard all reference types as pointer types. because the value of the reference type is two blocks, one is the value of an address saved in the stack, which is actually a pointer in C ++. (The 32-bit system contains two bytes), and the actual value is in heap, but the heap in C # is hosted by CRL, so you don't have to worry about allocating memory to release the memory. therefore, the reference type applies for heap memory with the new keyword in C ++ and then assigns the address value to a pointer.

Therefore, if the C # parameter is of the reference type, it is equivalent to the pointer parameter in C ++ or the reference type parameter.

Anyway, the reference type in C # is equivalent to the pointer type in C ++, or more accurately, it is a bit like a const type pointer, like a reference type in C ++.

The transfer by reference is similar to the transfer by reference in C ++.

 

 

 

Comparison between C ++ and C # "pointer Parameters"

We know that if a parameter is too large, it is not good to pass the value. In C ++, it will be passed using a pointer or reference. in C #, you don't have to worry about this problem at all, because all the complicated and non-basic types in C # Are reference types. When passing the reference type, you agree to pass a pointer.

However, suppose there is such a situation. if a parameter is large, it is passed by pointer or reference. but I know that it is convenient to know the pointer or reference, but it is worse than passing the value.The pointer or reference can change the value of the original parameter.In this way, there may be potential risks. In C ++, it is very simple. It is OK to directly use const to modify the pointer or reference.

But this problem may be a big problem in C.

Let's take an example. Suppose there is a class Arwen. there is a lot of data in it, and it is very important, it will occupy a lot of memory space. at some point, we want to pass the Arwen class as a parameter to a function. at the same time, because the data in the class is very important, we do not want to be modified in the function. two problems are involved here,

1. Do not pass the value of the parameter itself. Only the reference address is passed.

2. Do not modify the parameter value in the function

 

1. It is easier to skip all values.

In C #,

Void fun (Arwen)

{

// Do something

Return;

}

Arwen an = new Arwen ();

Fun (an); // This is OK. When an is passed, it is equivalent to a pointer pointing to the Arwen class.

 

In C ++, the pointer can be referenced.

Void fun (Arwen * an) {return ;}

Arwen * an = new Arwen ();

Fun ();

 

For question 2, do not modify the parameter value

C ++

Void fun (const Arwen *)

{

// Do something

Return;

}

 

What about Question 2 and C?

At the beginning, you also thought about adding a const like a pointer or reference in C ++. during compilation, there was an error. No. why not? First, C # references are similar to pointers and references. but after all, it is similar. in C #, there is no pointer concept. in addition, if you can modify the const parameter in the form parameter, it is required that the parameter you pass must be a const parameter. It is better to directly convert the parameter to a const parameter. directly const Arwen an; then fun (.

Therefore, the parameters in C # cannot be modified with any other stuff except ref and out. (In addition, const in C # is different from const in C ++. Const in C # has all the const attributes in C ++ and static attributes in C ++. in C #, readonly is similar to const in C ++)

So since C # cannot allow modification of the passed parameters in the function, how can this problem be solved? Is it dumb?

In fact, there is no way. it's just a very stupid way. instead of passing the reference of the Arwen class, the value is passed in. in C ++, you don't need to use pointers to transmit values. but we know that the reference type in C # is passed as a reference by default. How can we pass the value? It is definitely not possible. We can only declare another variable sbarwen first, and then copy the value in the Arwen an; object to the previous one. and then upload it to the function. and the entire copy function is required to be copied. if the class Arwen does not inherit iclonable, and a copy function is there, you cannot copy it directly. that's all. that's enough.

 

Therefore, for Question 1 and question 2, c ++ can completely solve problems, while C # cannot solve both problems at the same time. if you want to solve 2nd problems, you cannot solve problem 1, and you need to use a very stupid solution.

 

Related Article

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.