Pointer and reference const static

Source: Internet
Author: User

I read the discussion about pointers and references on the Internet today, and I have a bit of understanding. The record is as follows.

It is introduced in C ++ when it is referenced. It is essentially a pointer and is a special pointer.

1) It must be initialized and there is no null reference.

2) When being passed as a parameter, no parameters are required. The pointer effect is the same as the pointer reference. You can modify the pointer itself (that is, the pointer represents the memory address ).

Pass value: the role of the real parameter is not only for the initialization of the form parameter, but also has no effect.

But when the application is passed, there is no form parameter, and the object itself is involved.

In my opinion, the so-called address transfer is actually a fax, but it only refers to the address, and there are also parameters involved. Therefore, if the parameters themselves (instead of what the parameters point to) are modified, the real parameters cannot be synchronized. For example, if a pointer is a shortcut, you can modify the object pointed to by the shortcut, so the shortcut pointing to the object can be synchronized. However, if you modify a shortcut and direct it to another object, there is no connection between other shortcuts and the shortcut.

3) The referenced object cannot be changed. This is the same as const modification.

4) when it is returned, the reference will not trigger the copy constructor of the class.

Generally, a temporary variable is generated and the copy constructor of the class is called.

 

By the way, how can I initialize the const member variable in the class.

We all know that the const variable needs to be initialized, but how does one initialize the const member variable in the class?

In the constructor? This is definitely not acceptable. It is a value assignment instead of initialization. The answer is to initialize the constructor in the initialization list, which is executed before the constructor of the execution class. In this way, the object of each class can be initialized to different values.

 

Static variables are declared in. h and must be initialized in. cpp. The global location is preceded by the scope of the class, because it does not belong to an object, but all objects are shared and belong to the entire class. Static const is also initialized.

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.