Recently things more, in fact, is not busy, that is, things more affect the thoughts, so no mind to write articles.
Let's talk about some of the basics of functions today, and also explain the most confusing for beginners-when do you use pointer parameters?
function prototypes and Function definitions
As you all know, C + + to define a function, you need to declare the function prototype, for those who are accustomed to Java and other high-level languages, really feel that this is annoying.
The following code:
Copy Code code as follows:
Declaring a function prototype
void startgame (int param);
function definition
void startgame (int param)
{
Various logic
}
The function prototype is mainly for compilers, the function prototype is used to check function return value, number of parameters, parameter type and so on.
All in all, it's convenient to compile, the compiler is cool, we can be more cool.
But in practice, the function prototype is also convenient for us to quickly understand the function of a class.
These are very simple, no more nagging.
Two, const qualifier and pointer
There have been simple introduction to the const, such as const int num = 10; Then num is a constant and cannot be assigned again.
What if you use the const on the pointer?
Copy Code code as follows:
int num = 10;
const int *P = #
Compilation will complain
*p = 100;
As the code above, the error occurs when compiling, because the pointer p points to a const int type, which is a constant.
So the value of *p is a constant and cannot be modified.
Another reason, or there will be chaos:
1.P is a pointer
2.P points to a memory address where the value of a const int type is stored
3.*p represents the value that is stored in the memory address that P points to.
4. So, *p is a const int type of value
5. To sum up, *p cannot be assigned again.
Here to differentiate between P and *p, this is two concepts, one is a pointer, the other is the value that the pointer points to.
P can be assigned again, but the *p cannot be assigned a value.
Third, the function of the pointer parameters
Let's take a look at the following code:
Copy Code code as follows:
void notchangenum (int num);
void Changenum (int* num);
int _tmain (int argc, _tchar* argv[])
{
int num = 10;
This function does not change the value of num
Notchangenum (num);
cout << num << endl;
This function will change the value of num
int* p = #
Changenum (P);
cout << num << endl;
return 0;
}
void notchangenum (int num)
{
parameter is not a pointer
num = 999;
}
void Changenum (int* num)
{
parameter is a pointer
*num = 999;
}
Here are two functions, one is the normal parameter (value pass), the other is the pointer parameter (address delivery).
The first Notchangenum function does not change the value of NUM, because when NUM passes to the function, it copies a new value, and the original num is unaffected.
When you leave the Notchangenum function, the num parameter of the function is freed.
The parameter of the second Changenum function is the pointer, and we all know that the pointer points to a memory address, so the memory address that the parameter of the function points to is the memory address of Num.
Directly modifying the value on the memory address will affect the original NUM, so after leaving the Changenum function, Num's value will also be changed, the final value is 999.
This is the role of the pointer parameter, and in some cases we want to have a real effect on the modification of the parameters in the function.
Four, why to use pointer parameters
Why do you use pointers as arguments? Because the pointer can point directly to the memory address, you can modify the value directly in the function, and it still takes effect after leaving the function.
Say so, but, certainly some people will be confused, why? Why would you do that?
For example, our function argument is a class:
Copy Code code as follows:
void Play (Sprite* sp) {
}
Sprite and value are commonly used in cocos2d-x, and why are the parameters here pointers?
Because the parameter of the value reference is copied, this will not affect the original value, the copy will have an additional cost.
General class costs are relatively large (as opposed to basic types such as int, float, etc.), so copying a copy is not appropriate.
And we all need to change the sprite in the function of the coordinates, size and other attributes, if the use of value transfer, you can not modify the (only copy of the change).
Of course, this also depends on the specific project situation, I do not nag, too deep bad to blow water.
Five, do not want to copy, and do not want to change the value, how to do?
Large copy overhead, using pointer parameters is likely to be modified in the function of the value, how to do?
At this point, the const qualifier is used, the following code:
Copy Code code as follows:
void Play (const sprite* SP) {
}
This will not modify the value that the SP points to within the function, and it can avoid the extra overhead of value passing.
The variables inside the function are freed when they leave the function
As we said before, as long as the variable is not new, it will be released automatically when the scope is gone.
But let's take a look at this function:
Copy Code code as follows:
int Getnum () {
int num = 10;
return num;
}
Since the variable is released after it leaves the scope, NUM will be released after leaving the Getnum function.
What's the point of return num? Does the Getnum function really succeed in getting the number 10?
The answer is yes.
Because return returns NUM, it actually copies a copy of the value of the copy, and the original variable is released.
This is the secret of return.
However, the pointer is gone, look at the following code:
Copy Code code as follows:
Suppose there is such a structure
struct People {
int age;
};
people* Getnewpeople () {
People npeople;
Npeople.age = 20;
Return &nPeople;
}
This function returns a memory address that points to the type of people struct body.
According to the return rule, the returned time is actually a copy, but this time the copy is only a pointer, that is, a memory address.
This memory address still points to the npeople variable inside the function.
So even if the Getnewpeople function succeeds in returning a pointer, the value on the memory address that the pointer points to is still released.
In other words, what we get is just a wild pointer.
Vii. End
Well, this one is a bit bad, too much content, I just take part to blow the water ~