C + + Learning notes-const and static

Source: Internet
Author: User

Const

1. Use const to define constants

const int num = 10; should be initialized at declaration time, otherwise the value of the constant is indeterminate and cannot be modified

2, const and pointers

Pointer to constant (const decorates what the pointer points to)

Pointer to constant double rates[5] = {88.9, 100.12, 59.2, 111.1, 23.2};const double *pd = rates;cout << pd[2] << "\ n";pd [2] = 67.99;//errorcout << pd[2];

PD is a pointer to a constant,

You cannot use PD to modify the value it points to, but you can have PD point to a different address. A pointer to a constant is usually used as a function parameter.

pointer constant (const modifier pointer)

Pointer constant Double rates[5] = {88.9, 100.12, 59.2, 111.1, 23.2};cout << pd[2] << "\ n";d ouble * Const PD2 = RATES;PD2 [2] = 67.99;//okcout << pd[2];

The value of the PD2 is immutable and can only point to the address originally assigned to it, but PD2 may be used to change the data value it points to.

<<effective c++>> has a good way to remember: the const on the left side of the * number is the pointer of the content, the const on the right side of the number is a pointer.
Simple note is: Left content, right pointer.
You can also understand: ignore the type name first, and then see where the const is near, and modify who.
such as: const [INT] *p; First the int is removed, then the const modifier *P, p is the pointer, and *p is the object that the pointer is pointing to, immutable.

3. Const and Reference

Double value = 100;

Const Double & v = value;

You cannot modify the content that a const reference points to

4. Const and Class members

Class Staticandconsttest{public:staticandconsttest (int _num = +); ~staticandconsttest (void);//const member Constant of Class You must use an initialization list in the constructor method to initialize the const int num;}; staticandconsttest::staticandconsttest (int _num): num (_num) {}

Const member functions

Class Staticandconsttest{public:staticandconsttest (int _num = +); ~staticandconsttest (void);//const member function void Show () The const member of the const;//class must initialize the const int num with the initialization list in the constructor method; void Staticandconsttest::show () const{std::cout << num;}

The const member function cannot modify the calling object, as long as the class method does not modify the calling object, it should be declared as Const.

C + + Learning notes-const and static

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.