欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

C++中const修飾類成員怎么用

小編給大家分享一下C++中const修飾類成員怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都一家集口碑和實力的網(wǎng)站建設(shè)服務(wù)商,擁有專業(yè)的企業(yè)建站團隊和靠譜的建站技術(shù),十年企業(yè)及個人網(wǎng)站建設(shè)經(jīng)驗 ,為成都上1000家客戶提供網(wǎng)頁設(shè)計制作,網(wǎng)站開發(fā),企業(yè)網(wǎng)站制作建設(shè)等服務(wù),包括成都營銷型網(wǎng)站建設(shè),品牌網(wǎng)站制作,同時也為不同行業(yè)的客戶提供做網(wǎng)站、成都做網(wǎng)站的服務(wù),包括成都電商型網(wǎng)站制作建設(shè),裝修行業(yè)網(wǎng)站制作建設(shè),傳統(tǒng)機械行業(yè)網(wǎng)站建設(shè),傳統(tǒng)農(nóng)業(yè)行業(yè)網(wǎng)站制作建設(shè)。在成都做網(wǎng)站,選網(wǎng)站制作建設(shè)服務(wù)商就選成都創(chuàng)新互聯(lián)。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
/*

  • A1 const 修飾一個類中成員變量,則此變量必要用初始化列表來進行初始化。

  • A2 const 修飾一個類中成員函數(shù),則表示此成員函數(shù)不會修改類中所有成員變量。

  • A3 const 修飾一個函數(shù)入?yún)⒌囊脮r,表示此引用的內(nèi)容不得在此函數(shù)中發(fā)生變化。

  • A3.1 const 修飾一個函數(shù)的“引用入?yún)ⅰ睍r,那么這個函數(shù)只可以使用入?yún)⒌?nbsp;const 的成員函數(shù)

  • */
    class Student{

    string m_name;
    const int m_id; //A1
    int m_score;
    public:
    int show_info_no_const(void)
    {
    cout  <<"no const function: name "<< m_name << ", id " << m_id<<endl;
    return 0;
    }
    int show_info(void) const
    {
    cout  <<"name "<< m_name << ", id " << m_id<<endl;
    //  m_score=90; //A2 此處會有編譯錯誤,原因就是show_info 被const來修飾了。
    return 0;
    }
    void set_name(char new_name);
    ~Student();
    Student():m_id(999){};
    Student(const char
    h);
    Student(const Student &obj);
    };
    Student::Student(const Student &obj):m_id(888)//A1
    {
    cout <<"call copy constructor "<<endl;
    m_name = obj.m_name;
    }
    #if 1
    Student::Student(const char* h):m_id(777)
    {
    m_name=h;

}
#endif

#if 1
Student::~Student()
{
}

void Student::set_name(char* new_name)
{
m_name=new_name;
//m_id=9;//A1 m_id是不可以再被修改的。

}
#endif
//測試A3.1
void show_student(const Student& Stu) //A3
{
Stu.show_info();
//  Stu.show_info_no_const(); //A3.1 此函數(shù)實際上沒有改變Stu的值 ,但是由于它沒有使用const進行修飾,所以發(fā)生了const屬性丟失的情況。
}
int main()
{
Student A1;
A1.set_name("A1");
A1.show_info();
Student A2("abl");
A2.show_info();

Student A3(A2);
A3.show_info();

return 0;

}

以上是“C++中const修飾類成員怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

新聞標題:C++中const修飾類成員怎么用
本文網(wǎng)址:http://chinadenli.net/article38/ihihsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站制作、、網(wǎng)站策劃、網(wǎng)站導(dǎo)航、網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計