国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

深入淺析c/c++ 中的static關(guān)鍵字

2020-01-26 13:39:09
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

static關(guān)鍵字

1,static 成員變量

static 成員變量不隨著對(duì)象的創(chuàng)建而開(kāi)辟內(nèi)存空間。也就是說(shuō),不管從哪個(gè)對(duì)象去看static成員變量,都是一樣的。

2, static 成員方法

static 成員方法里不可以調(diào)用非static 成員方法。

非static 成員方法里可以調(diào)用static 成員方法。

原因:非static 成員方法里是沒(méi)有this指針的,所以在里面調(diào)用非static 成員方法時(shí),無(wú)法傳遞this指針。static 成員方法不需要this指針。

重點(diǎn):初始化static成員變量,要在類(lèi)的外面。

#include <iostream>using namespace std;class Test{ friend void fun(const Test &t);public: Test(int d = 0) : data(d){  count++; } ~Test(){  count--; }private: int data; static int count;};void fun(const Test &t){ cout << t.data << endl; cout << Test::count << endl;}//初始化static成員變量 int Test::count = 0;int main(){ //int Test::count = 0;//編譯不過(guò),必須在外面初始化static成員變量 //Test::count = 0;//編譯不過(guò),必須在外面初始化static成員變量 Test t(10); fun(t); Test t1(11); fun(t1);}

總結(jié)

以上所述是小編給大家介紹的c/c++ 中的static關(guān)鍵字,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久治县| 安顺市| 紫金县| 榆林市| 阳泉市| 阜宁县| 三台县| 南丹县| 三亚市| 巢湖市| 台南县| 田阳县| 贵溪市| 宽城| 余干县| 塘沽区| 靖宇县| 广德县| 济阳县| 蚌埠市| 湘阴县| 铜川市| 民丰县| 永寿县| 芦溪县| 芷江| 德昌县| 金寨县| 南溪县| 阿克| 温泉县| 绥宁县| 疏勒县| 昌图县| 亚东县| 兴和县| 巨鹿县| 滦南县| 临汾市| 玛纳斯县| 台南市|