Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. import std.stdio;
  2.  
  3.  
  4. void immutableTest() {
  5. immutable int N = 10;//変更不可能
  6. immutable int* ptr = &N;//変更不可能なポインタ
  7. //ptr = &N;当然不可能
  8.  
  9. // このコードは動かない
  10. // immutableなポインタはimmutableな変数しかとれない
  11. /*
  12. int M = 10;
  13. immutable int* ptr2 = &M;
  14. writeln(M);
  15. assert(M == 10);
  16. M = 20;
  17. */
  18. }
  19.  
  20. void constTest() {
  21. const int N = 10;
  22. const int* ptr = &N;
  23. //ptr = &N;当然不可能
  24.  
  25.  
  26. int M = 10;
  27. const int* ptr2 = &M;
  28. writeln(M);
  29. assert(M == 10);
  30. M = 20;
  31. assert(M == 20);
  32. //*ptr = 100;これはできないけど
  33. M = 100;
  34. assert(*ptr2 == 100);
  35. }
  36.  
  37. void main() {
  38. immutableTest;
  39.  
  40. constTest;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement