Last active
February 5, 2016 17:17
-
-
Save alphaKAI/5fe2e6a4122182d02051 to your computer and use it in GitHub Desktop.
Immutable と constの簡単な違い
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
void immutableTest() { | |
immutable int N = 10;//変更不可能 | |
immutable int* ptr = &N;//変更不可能なポインタ | |
//ptr = &N;当然不可能 | |
// このコードは動かない | |
// immutableなポインタはimmutableな変数しかとれない | |
/* | |
int M = 10; | |
immutable int* ptr2 = &M; | |
writeln(M); | |
assert(M == 10); | |
M = 20; | |
*/ | |
} | |
void constTest() { | |
const int N = 10; | |
const int* ptr = &N; | |
//ptr = &N;当然不可能 | |
int M = 10; | |
const int* ptr2 = &M; | |
writeln(M); | |
assert(M == 10); | |
M = 20; | |
assert(M == 20); | |
//*ptr = 100;これはできないけど | |
M = 100; | |
assert(*ptr2 == 100); | |
} | |
void main() { | |
immutableTest; | |
constTest; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment