C + + const keyword Decoration reference

Source: Internet
Author: User

//two usages of const-modified references#include <iostream>using namespacestd;structteacher{Charname[ -]; intAge ;};voidSetA (ConstTeacher &T1) {    //t1.age = 13; //Error C3490: cannot be modified because "age" is being accessed through a const object}voidMain () {//The first way to useTeacher t; T.age= A;    SetA (t); //The second way to use//int &b = 10; //Error Error C2440: "Initialize": Cannot convert from "int" to "int &"//because the reference is essentially a constant pointer cannot take the address of the literal 10, error    Const int&b =Ten; //When a const reference is initialized with a constant (literal), the C + + compiler allocates a memory space for the constant value,//and assigns the address of the memory space to the reference//B = 11; //Error C3892: "B": Cannot assign a value to a constantSystem"Pause");}

C + + const keyword Decoration reference

Related Article

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.