Before Syria

From Java to C++

Style to reverse

In the first two, the comparison of Java code examples, to do a rapid C++ transformation, but in fact, that is not enough, because in any language, the final fight is to deal with the details, why C++ is so difficult to learn, in fact, there are enough details, knowledge points are complex enough, so we will focus on C++, Is also relative to Java unique some features, one by one to learn, of course, there will be corresponding code examples, to help understand. We all know that Java has value passing and reference passing, but in C++, we have pointer passing. What’s going on here? Let’s take a look.

Value passed

Features:

  • Parameters are copies of arguments, and changing the value of a parameter does not affect the value of the external argument.
  • Value passing is used when parameters need to be modified internally in a function and the change is not expected to affect the caller.

Pointer passed

Features:

  • A parameter is a pointer to the address of the argument. A pointer to a parameter is an operation on the argument itself
  • The *a method operates on the pointer
  • When calling a method, you need to decorate the & argument

Why modify the argument with &

This is related to the role of ampersand and * in modifying variable names

  • &b takes the value address of B, which is defined as the address-fetch operator
  • * A takes the value of a, which is defined as the ** indirection operator that returns the value of the variable at the address specified by the operand

In pointer passing, the address &b is passed in, and then in this function, the value of the address of &b is fetched by *a, and related operations are performed. It’s a little convoluted, but understandable.

reference

Features:

  • Parameters are aliases for the arguments. Operations on parameters are operations on the arguments
  • Line arguments are local variables that open up memory on the stack and point to the address of the argument variable
  • Anything done to a parameter affects the argument variable

The difference between Pointers and references

Pointers and references can both change the value of an argument with a row argument, so what’s the difference? In fact, this has a lot to do with the rule of reference, let’s look at the rule of reference:

  • A reference must be initialized before it is created
  • There cannot be NULL references
  • Once the application is initialized, the reference relationship cannot be changed

There are no limits to the pointer rules. Why is that? In fact, pointer passing is essentially the way of value passing, it is passing an address value. Actually a lot of people to understand this is not so good, you can understand, so we all know Java multi-thread, operation of the Shared memory variable, actually from Shared memory copy to yourself in the area of memory, the equivalent of a copy of the final finished operation synchronization to the Shared memory, to achieve a closed loop, you can be so understand pointer, But it doesn’t work that way, just for your understanding.

How to choose

You can use Pointers as more flexible references, when the reference rules are not met, that’s all.