本文章向大家介紹如何正確的使用C++ 共享數(shù)據(jù)保護(hù)機(jī)制,主要包括{**}的使用實(shí)例,應(yīng)用技巧,基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下。

(1)常類型
①常對(duì)象:必須進(jìn)行初始化,不能被更新。
const 類名 對(duì)象名
②常成員
用const進(jìn)行修飾的類成員:常數(shù)據(jù)成員和常函數(shù)成員
③常引用:被引用的對(duì)象不能被更新。
const 類型說(shuō)明符 &引用名
④常數(shù)組:數(shù)組元素不能被更新(詳見(jiàn)第6章)。
類型說(shuō)明符 const 數(shù)組名[大小]...
⑤常指針:指向常量的指針(詳見(jiàn)第6章)。
用const修飾的對(duì)象
例:
class A
{
public:
A(int i,int j) {x=i; y=j;}
...
private:
int x,y;
};
A const a(3,4); //a是常對(duì)象,不能被更新用const修飾的對(duì)象成員
①常成員函數(shù)
使用const關(guān)鍵字說(shuō)明的函數(shù)。
常成員函數(shù)不更新對(duì)象的數(shù)據(jù)成員。
常成員函數(shù)說(shuō)明格式:
類型說(shuō)明符 函數(shù)名(參數(shù)表)const;
這里,const是函數(shù)類型的一個(gè)組成部分,因此在實(shí)現(xiàn)部分也要帶const關(guān)鍵字。
const關(guān)鍵字可以被用于參與對(duì)重載函數(shù)的區(qū)分
通過(guò)常對(duì)象只能調(diào)用它的常成員函數(shù)。
②常數(shù)據(jù)成員
使用const說(shuō)明的數(shù)據(jù)成員。
//常成員函數(shù)舉例
#include<iostream>
using namespace std;
class R {
public:
R(int r1, int r2) : r1(r1), r2(r2) { }
void print();
void print() const;
private:
int r1, r2;
};
void R::print() {
cout << r1 << ":" << r2 << endl;
}
void R::print() const {
cout << r1 << ";" << r2 << endl;
}
int main() {
R a(5,4);
a.print(); //調(diào)用void print()
const R b(20,52);
b.print(); //調(diào)用void print() const
return 0;
}//常數(shù)據(jù)成員舉例
#include <iostream>
using namespace std;
class A {
public:
A(int i);
void print();
private:
const int a;
static const int b; //靜態(tài)常數(shù)據(jù)成員
};
const int A::b=10;
A::A(int i) : a(i) { }
void A::print() {
cout << a << ":" << b <<endl;
}
int main() {
//建立對(duì)象a和b,并以100和0作為初值,分別調(diào)用構(gòu)造函數(shù),
//通過(guò)構(gòu)造函數(shù)的初始化列表給對(duì)象的常數(shù)據(jù)成員賦初值
A a1(100), a2(0);
a1.print();
a2.print();
return 0;
}如果在聲明引用時(shí)用const修飾,被聲明的引用就是常引用。
常引用所引用的對(duì)象不能被更新。
如果用常引用做形參,便不會(huì)意外地發(fā)生對(duì)實(shí)參的更改。常引用的聲明形式如下:
const 類型說(shuō)明符 &引用名;
//常引用作形參
#include <iostream>
#include <cmath>
using namespace std;
class Point { //Point類定義
public: //外部接口
Point(int x = 0, int y = 0)
: x(x), y(y) { }
int getX() { return x; }
int getY() { return y; }
friend float dist(const Point &p1,const Point &p2);
private: //私有數(shù)據(jù)成員
int x, y;
};
float dist(const Point &p1, const Point &p2) {
double x = p1.x - p2.x;
double y = p1.y - p2.y;
return static_cast<float>(sqrt(x*x+y*y));
}
int main() { //主函數(shù)
const Point myp1(1, 1), myp2(4, 5);
cout << "The distance is: ";
cout << dist(myp1, myp2) << endl;
return 0;
}以上就是小編為大家?guī)?lái)的如何正確的使用C++ 共享數(shù)據(jù)保護(hù)機(jī)制的全部?jī)?nèi)容了,希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,!
網(wǎng)站題目:如何正確的使用C++共享數(shù)據(jù)保護(hù)機(jī)制-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)地址:http://chinadenli.net/article2/cohdic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、響應(yīng)式網(wǎng)站、定制開(kāi)發(fā)、搜索引擎優(yōu)化、微信公眾號(hào)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容