Document directory
- No Default real parameters in C #
Function return value
When defining a function, a type of return value is specified. A matching type must be returned in the function body, or a type that can be matched after implicit conversion. however, sometimes, if the function body contains some branch statements, a value can be returned, but sometimes the return value is not returned. let's take an example.
The following C ++ functions are available:
Int funarwen (int A, int B)
{
If (A <B)
Return 888;
}
Obviously, the above function sometimes returns a value, but sometimes it does not.
Int result = funarwen (888); // The result is;
Int res = funarwen (5, 4 );
// At this time, the function will not return values. However, when I run the function in Vs, the res value is 5. It seems that the value of the first parameter is returned.
// However, this is definitely a wrong result.
During function compilation in C ++, a warning message "Not all control paths return a value" is generated. If the function in C # cannot be compiled, an error is returned, "Not all code paths return a value ".
You must write if (a <B) Return 888;
Else return 44;
Or if (a <B) Return 888; return 444;
In this way
C # has made some improvements in many areas of C ++ and made some checks. this makes the code you write less error-prone. in fact, the development of a programming language often focuses on simplifying some complex syntaxes so that you can use less code to implement more functions. the other is to do more checks and check for some errors during compilation. of course, to be accurate, some errors are not true. As long as there are potential mistakes, they may be treated as errors.
We usually do not often talk about software testing. In fact, the compiler's work is also a kind of testing. We will find out the syntax errors in the code for you. if any errors can be found during compilation, it would be perfect, but the compiler is not so clever. many runtime errors cannot be detected.
C ++ returns the pointer or reference of a local variable in the local function body.
In addition, it is worth noting that in C ++, if the returned result is a local pointer or reference type in the function body. an error occurs because the local variables in the function body are released when they exceed the scope. so the pointer becomes a wild pointer. I don't know where to go. for example
Int * getresult ()
{
Int num = 110;
Int * IP = & num;
Return IP;
}
Int * P = getresult (); // this operation is incorrect, but sometimes incorrect operations will get the correct result.
// You may encounter a running error during the running process. If you run the task correctly, you may obtain an incorrect result. anyway, I got a correct result during the vs test.
Because num is out of scope and tells the system that the memory is not needed, the system can allocate the memory to others. however, if the memory has not been assigned to anyone yet, the value is still there, so you may still get a correct value when accessing the value through the pointer.
Default real parameters
If there are many parameters in a function, it is very troublesome to include so many parameters during the call, and some parameters are not so important. We can just set the default value. the first thing you may think of is to overload a function. Many functions come out, but the number of parameters is different. obviously, this is indeed a solution. but the implementation is not beautiful enough. the default real parameter in C ++ can better solve this problem. example
Void register (string username = "Soy Sauce"
, String Pwd = "123"
, Int age = 18
)
{
// Do something
}
In this way, we can call a function as follows:
Register ();
Register ("Arwen ");
Register ("Arwen", "911 ");
Register ("weiwen", "110", 24 );
All the above calls are correct, but you cannot call them in this way.
Register ("wei", 24); // an error will occur because the system matches from left to right. If you pass two parameters, it only matches username and PWD.
No Default real parameters in C #
Unfortunately, C #2.0 and 3.0 do not provide this function. it is said that C #4.0 has introduced this function. but I have never used it. 4. 0. it is too fast for Microsoft to update things. It will be very tiring to keep things fashionable. let's talk about the basics honestly.
This pointer
For example, there are c ++ class Arwen
Class Arwen
{
PRIVATE:
Int age;
Int height;
Public:
Void setage (INT age)
{
This-> age = height;
}
Void setheight (INT HgT)
{
Height = HgT;
}
}
Arwen Wen;
Int HgT = 111;
Wen. setheight (HgT); // we know that functions of each class do not belong to a specific instance like static variables, so the instance object uses a common function. the function is saved in the code area. so how can we find a function when calling a function like this? In fact, when you call a function like above, the compiler will convert it
Arwen: setheight (& Wen, HgT );
Then assign the value of height to Wen. Height = HgT; // by default, the instance object is prefixed before the self-member variable. & Wen is the this pointer.
However, if the member variable and the passed parameter have the same name, the this pointer must be explicitly added.
Like this-> age = age in setage;
Const Function
We can add a const after the function, such as void setage (INT age) const {This-> age = age ;}
Arwen Wen;
Int age = 222;
Wen. setage (AGE); // an error occurred.
Because the compiler converts the this pointer in the above call to a pointer of the const type, this-> age = age is not needed to modify the value pointed to by the pointer.
The usage and principle of this in C # is similar to that in C ++. although C # does not have the pointer concept, I guess many background implementations are ultimately implemented through pointers. it is estimated that the C # compiler is also developed using C ++ and C. even Microsoft's operations are developed in C ++ and C.