全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1225
推到 Plurk!
推到 Facebook!

struct的成員如何讓它指定完之後,不再讓它受變更?

缺席
Randgris
一般會員


發表:20
回覆:30
積分:10
註冊:2007-04-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-05-06 15:57:21 IP:140.126.xxx.xxx 未訂閱
<textarea class="cpp" rows="10" cols="60" name="code"> #include #include struct student { int a; }; int main() { struct student s1; s1.a = 5; printf("%d\n", s1.a); system("pause"); return 0; } </textarea>
s1.a = 5; 如何讓它變成const?
編輯記錄
Randgris 重新編輯於 2007-05-07 22:59:26, 註解 無‧
Randgris 重新編輯於 2007-05-07 23:00:25, 註解 無‧
Randgris 重新編輯於 2007-05-07 23:01:51, 註解 無‧
Randgris 重新編輯於 2007-05-07 23:02:29, 註解 無‧
Randgris 重新編輯於 2007-05-07 23:03:23, 註解 無‧
harpist
資深會員


發表:3
回覆:251
積分:430
註冊:2002-10-03

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-05-07 01:31:00 IP:211.74.xxx.xxx 未訂閱
一般來說 const  這個型別修飾詞會將一個 object  轉換成一個常數,也就是被視為唯讀。
各家編譯器內部實作可能不同,但是往往當你宣告了一個 const 常數 然後又嘗試在程式裡去改變它的值時都會導致編譯錯誤。
如果我們要定義一個 const 常數但又要改變它的值是否可行呢? 雖然有點矛盾
1.定義 const 常數但先不指定常數值
2.利用const_cast 強制轉換
這個可能會是你要的東西,不過這種設計並不好

<textarea class="cpp" rows="10" cols="60" name="code">//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include #include #include #include <Forms.hpp> struct student { const int a; //[C Warning]"Constant member 'student::a' is not initialized" student(){}; }; //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif</textarea>

<textarea class="cpp" rows="10" cols="60" name="code">//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { student student1; ShowMessage(student1.a); *const_cast(&student1.a)=7; ShowMessage(student1.a); *const_cast(&student1.a)=8; ShowMessage(student1.a); //student1.a = 99; //這樣用會[C Error]"Cannot modify a const object" } //--------------------------------------------------------------------------- </textarea>



------
~§~迷時師渡,悟了自渡~§~
編輯記錄
harpist 重新編輯於 2007-05-07 01:38:35, 註解 無‧
harpist 重新編輯於 2007-05-07 02:03:01, 註解 無‧
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-05-07 09:42:02 IP:122.124.xxx.xxx 未訂閱
PO程式碼的方式請參考版規說明,煩請修改謝謝配合

===================引 用 Randgris 文 章===================
#include
#include

struct student {
int a;
};

int main() {
struct student s1;

s1.a = 5;

printf("%d\n", s1.a);

system("pause");
return 0;
}

s1.a = 5;
如何讓它變成const?

syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-05-08 13:42:09 IP:61.64.xxx.xxx 訂閱
是不是 const 由程式設計師認定
所以實際意義不大
使用 const 多是方便編譯器幫你處理並提醒你

但身為一個「好的」程式設計者,應該隨時知道自己在做什麼,不然,再好的機制,也是會被槁爛

看坊間一堆,動不動就掛點的程式,就是很好的例子
系統時間:2024-05-08 0:50:20
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!