"Thinkinginc++" 39, Const's delivery and return address

Source: Internet
Author: User

of the pointerConstThere are two kinds:ConstModifies the object that the pointer is pointing to, or modifies the address stored inside the pointer.


/*** book: "thinkinginc++" * Function: Const's delivery and return address * Time: September 7, 2014 14:11:41* Author: cutter_point*///parameter is non-const type void T (int*) {}//     The parameter is a const type of void U (const int* CIP)//CIP is a pointer to the const int data {//! *cip=2;     Illegal operation, because the value of CIP point is modified by const, cannot change int i=*cip; Copy value, OK//!   int* Ip2=cip; Illegal operation, cannot assign const data to non-const type}//return const type pointer address const char* V () {return "cutter_point--char* V ()";}    Returns a const address and a const value of type const int* Const W () {static int i; return &i;}    int main () {int x=0;    int* ip=&x;    Const int* cip=&x;    T (IP); //! T (CIP);    The const data cannot be assigned to a non-const function u (IP) when the parameter is not evaluated.     U (CIP); Because it is a parameter copy mechanism, the const parameter can be given to the const type and non-const type/* If you pass or return an address (a pointer or a reference), it is possible for the client programmer to fetch the address and modify its initial value. If you make this pointer or reference a const, it will prevent the occurrence of such things, the address const return is to return a const pointer can not be assigned to non-const *///! char* cp=v ();    The return value of function V () can only be assigned to a const pointer, and the value of the pointer is Const const CHAR* ccp=v (); //!   int* ip2=w ();    The return value of W () is const const int* const CCIP=W ();     Const int* cip2=w (); This is also possible, I am also drunk, C + + really mysterious not to test return 0;}


"Thinkinginc++" 39, Const's delivery and return address

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.