c++實(shí)例研究
從0開(kāi)始
C++博客
::
首頁(yè)
::
新隨筆
::
聯(lián)系
::
聚合
::
管理
::
104 隨筆 :: 0 文章 :: 20 評(píng)論 :: 0 Trackbacks
<
2010年4月
>
日
一
二
三
四
五
六
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
公告
常用鏈接
我的隨筆
我的評(píng)論
我參與的隨筆
留言簿
(1)
給我留言
查看公開(kāi)留言
查看私人留言
隨筆分類(lèi)
c++面試題(8)
(rss)
c++實(shí)例(45)
(rss)
django
(rss)
POJ(3)
(rss)
Unix 命令(8)
(rss)
web開(kāi)發(fā)(19)
(rss)
工程問(wèn)題集(10)
(rss)
雜談(2)
(rss)
隨筆檔案
2012年2月 (1)
2011年6月 (1)
2011年3月 (1)
2010年11月 (6)
2010年10月 (28)
2010年9月 (2)
2010年7月 (5)
2010年6月 (24)
2010年5月 (31)
2010年4月 (5)
參考
besterChen
Make手冊(cè)
高性能編程參考站
我的幾個(gè)分站
CSDN站
GoogleAppEngine站
JavaEye站
北郵人站
豆瓣站
新浪站
搜索
最新評(píng)論
1.?re: 手機(jī)九點(diǎn)密碼鎖的可能性有多少種?
請(qǐng)說(shuō)出你所想到的幾種可能
--煩惱的鴨子
2.?re: 手機(jī)九點(diǎn)密碼鎖的可能性有多少種?
我的手機(jī)密碼忘記了,各位幫幫忙
--煩惱的鴨子
3.?re: 枚舉類(lèi)型和整形的轉(zhuǎn)換和比較
ww
--sss
4.?re: POJ 1062 拓?fù)渑判?/a>
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--npbool
5.?re: POJ 1062 拓?fù)渑判?/a>
這不是杭電上的題吧。
--Ancowei
閱讀排行榜
1.?[zz]linux sleep用法(7459)
2.?svn 提交時(shí) 文件夾 missing 問(wèn)題的解決(7358)
3.?gdb調(diào)試g++ -g生成文件,list后不能看到源代碼(4822)
4.?gdb用十六進(jìn)制打印變量值(3851)
5.?枚舉類(lèi)型和整形的轉(zhuǎn)換和比較(2701)
評(píng)論排行榜
1.?POJ 1062 拓?fù)渑判?4)
2.?手機(jī)九點(diǎn)密碼鎖的可能性有多少種?(2)
3.?函數(shù)指針的取地址和解引用(2)
4.?很有挑戰(zhàn)性的題目(不斷更新)(2)
5.?運(yùn)算符||的結(jié)合律是從左往右還是從右往左(2)
保持const member function,實(shí)現(xiàn)修改成員變量的3種方法
#include
<
iostream
>
#include
<
cstdlib
>
using
namespace
std;
class
Foo
{
public
:
Foo(
int
n):val(n),changed(
false
)
{}
int
getVal()
const
{
Foo
*
fp
=
const_cast
<
Foo
*>
(
this
);
fp
->
changed
=
true
;
//
changed=true;
//
error: in read-only structure
return
val;
}
;
bool
isVisit()
const
{
return
changed;}
private
:
int
val;
bool
changed;
}
;
int
main()
{
Foo f(
10
);
cout
<<
f.isVisit()
<<
endl;
cout
<<
f.getVal()
<<
endl;
cout
<<
f.isVisit()
<<
endl;
system(
"
PAUSE
"
);
return
0
;
}
一種更好的方法是使用mutable關(guān)鍵字,表示即使在const情況下,仍然能被修改
#include
<
iostream
>
#include
<
cstdlib
>
using
namespace
std;
class
Foo
{
public
:
Foo(
int
n):val(n),changed(
false
)
{}
int
getVal()
const
{
changed
=
true
;
//OK
return
val;
}
;
bool
isVisit()
const
{
return
changed;}
private
:
int
val;
mutable
bool
changed;
}
;
int
main()
{
Foo f(
10
);
cout
<<
f.isVisit()
<<
endl;
cout
<<
f.getVal()
<<
endl;
cout
<<
f.isVisit()
<<
endl;
system(
"
PAUSE
"
);
return
0
;
}
如果一個(gè)類(lèi)中有很多需要被修改,可以單獨(dú)作為一個(gè)成員類(lèi)
#include
<
iostream
>
#include
<
cstdlib
>
using
namespace
std;
class
Bar
{
public
:
Bar():changed(
false
),val_count(
0
)
{}
bool
changed;
int
val_count;
}
;
class
Foo
{
public
:
Foo(
int
n):val(n),b(
new
Bar())
{}
int
getVal()
const
{
b
->
changed
=
true
;
//
ok
b
->
val_count
++
;
return
val;
}
;
bool
isVisit()
const
{
return
b
->
changed;}
int
getValCount()
const
{
return
b
->
val_count;}
private
:
int
val;
Bar
*
b;
}
;
int
main()
{
Foo f(
10
);
cout
<<
f.isVisit()
<<
endl;
cout
<<
f.getValCount()
<<
endl;
cout
<<
f.getVal()
<<
endl;
cout
<<
f.isVisit()
<<
endl;
cout
<<
f.getValCount()
<<
endl;
cout
<<
f.getVal()
<<
endl;
cout
<<
f.isVisit()
<<
endl;
cout
<<
f.getValCount()
<<
endl;
system(
"
PAUSE
"
);
return
0
;
}
特別注意上例中,指針b的初始化的寫(xiě)法,其實(shí),在構(gòu)造函數(shù)冒號(hào)后的member(val)相當(dāng)于member=val
posted on 2010-05-01 11:10
elprup
閱讀(387)
評(píng)論(0)
編輯
收藏
引用
所屬分類(lèi):
c++實(shí)例
只有注冊(cè)用戶(hù)
登錄
后才能發(fā)表評(píng)論。
【推薦】100%開(kāi)源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
成員變量的地址
struct對(duì)齊一題
類(lèi)僅有復(fù)制構(gòu)造函數(shù)時(shí),將覆蓋默認(rèn)構(gòu)造函數(shù)和復(fù)制構(gòu)造函數(shù)
c++異常:多個(gè)catch只走一個(gè)分支
正負(fù)對(duì)模運(yùn)算的影響
運(yùn)算符重載回憶代碼
自定義析構(gòu)函數(shù)和delete &object不能共存
copy構(gòu)造函數(shù)可以訪問(wèn)同類(lèi)型參數(shù)的私有成員,卻不能訪問(wèn)非同類(lèi)的參數(shù)的私有成員
隱式的copy構(gòu)造函數(shù)
c++沉思錄 代碼集 2
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問(wèn)
Chat2DB
管理
Powered by:
C++博客
Copyright © elprup
久久乐国产精品亚洲综合
|
狠狠色丁香久久婷婷综合蜜芽五月
|
久久婷婷人人澡人人爽人人爱
|
久久午夜福利电影
|
亚洲精品高清国产一线久久
|
亚洲av日韩精品久久久久久a
|
久久久久人妻精品一区
|
尹人香蕉久久99天天拍
|
国产成人精品久久
|
久久精品国产亚洲网站
|
久久人人爽人人人人片av
|
99久久国语露脸精品国产
|
久久精品亚洲精品国产欧美
|
久久精品无码一区二区WWW
|
2021国内精品久久久久久影院
|
欧洲人妻丰满av无码久久不卡
|
久久精品亚洲精品国产欧美
|
国内精品综合久久久40p
|
国产2021久久精品
|
99久久人妻无码精品系列
|
亚洲精品无码专区久久同性男
|
久久99精品久久久久久
|
99精品国产99久久久久久97
|
国产精品美女久久久m
|
亚洲va久久久噜噜噜久久狠狠
|
亚洲国产婷婷香蕉久久久久久
|
伊人久久大香线蕉影院95
|
久久精品欧美日韩精品
|
色偷偷88欧美精品久久久
|
国产精品亚洲综合专区片高清久久久
|
亚洲精品美女久久777777
|
久久久久99精品成人片牛牛影视
|
99国产欧美精品久久久蜜芽
|
中文字幕日本人妻久久久免费
|
欧美亚洲国产精品久久
|
久久精品女人天堂AV麻
|
久久久这里有精品中文字幕
|
久久综合日本熟妇
|
中文字幕精品久久
|
77777亚洲午夜久久多人
|
午夜天堂精品久久久久
|