[C++]const Box * p | | Box const * p | | The difference between Box * Const P

Source: Internet
Author: User

When const is used in conjunction with pointers, it is easy to confuse:

1. Can the const limit the pointer to other memory?

2. Is it forbidden to modify the contents of the block of memory it points to by this pointer? (PS: This is said to prohibit the use of this pointer to modify the memory block, so it is not impossible to modify, but to be modified by other means.) )


Let's look at the underlying code first.

Box.h Author:[email protected] #ifndef _box_h#define _box_hclass BOX {public:box (); int volume (); void print () const; voi d setLength (int) const;void setwidth (int); ~box ();p rivate:mutable int length; mutable: int width;int height can be modified in the const function;}; #endif//Box.cpp#include <iostream> #include "Box.h" using namespace std; Box::box () {length = width = height = 0;cout << "box" << This << "constructor () executed." << End l;} Box::~box () {cout << "box" << this << "Deconstructor () executed." << Endl;} void box::setlength (int l) const{length = l;} void Box::setwidth (int w) {width = w;} int Box::volume () {return length * width * height;} void Box::p rint () const {cout << "len=" << length << "width=" << width << "height=" <&L T Height << Endl;}

The underlying code above. Now let's look at:

Box * Const Pbox const * PCONST box * p

The difference between the three.


Box * Const P

Box box, bother; There is no need for new to initialize the Box.print (); Box * Const P1 = &BOX;P1 = &bOther; Error:read-only variable is not assignable

When the code is above, according to the hint of "error:read-only variableis not assignable", it can be determined that the const modifier is the pointer to its left, meaning that after the P1 initialization of the pointer, it is forbidden to point to another address.



Box Const * P

Box box, bother; There is no need for new to initialize the Box.print (); Box Const * P1 = &BOX;P1 = &bOther; You can re-point to a new address, p1->setlength, or//const-Modified function, which modifies only the member variable of mutable p1->setwidth (20); Error:member function ' setwidth ' not viable://' this ' argument have type ' const Box ', but function was not marked const

When the code is above, it is found that P1 can be re-assigned to another memory address. However, when you call the non-const function of box, you will be prompted for error. So determining the Const modifier meaning is to prohibit the content of the memory block that it points to by P1 pointers.


Const BOX * p

Box box, bother; There is no need for new to initialize the Box.print (); Const BOX * p1 = &BOX;P1 = &bOther; You can re-point to a new address, p1->setlength, or//const-Modified function, which modifies only the member variable of mutable p1->setwidth (20); Error:member function ' setwidth ' not viable://' this ' argument have type ' const Box ', but function was not marked const

When the code is above, it is consistent with the box const * p case.

Finally, if the const Box is const p, then the pointer p is forbidden to point to other addresses, and the contents of the memory block pointed to by the pointer p are forbidden.


Summarize:

The decorated format of the member function that is associative to const is:

void box::setlength (int l) const{//const is placed at the last length = l;}

The const is at the bottom and is invalid when the const is placed at the front of the function, so it is considered to be a const modifier that modifies the content on its left side, but when Const itself is the leftmost modifier, it works on the right.

[C++]const Box * p | | Box const * p | | The difference between Box * Const P

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.