001 /****************************/ 002 /* new 演算子と代入・初期化 */ 003 /* coded by Y.Suganuma */ 004 /****************************/ 005 #include <stdio.h> 006 #include <vector> 007 008 using namespace std; 009 010 /*****************/ 011 /* クラスComplex */ 012 /*****************/ 013 class Complex 014 { 015 public: 016 int x, y[1], *z; 017 Complex () 018 { 019 x = 0; 020 y[0] = 0; 021 z = new int [1]; 022 z[0] = 0; 023 } 024 }; 025 026 /********/ 027 /* main */ 028 /********/ 029 int main() 030 { 031 // 単純変数 032 printf("***単純変数***\n"); 033 int a1 = 0; 034 int a2 = a1; 035 a2 = 1234; 036 printf(" a1 %d\n", a1); 037 printf(" a2 %d\n", a2); 038 printf("***単純変数(new)***\n"); 039 int *p1 = new int(0); 040 int *p4 = p1; 041 *p4 = 1234; // p1 が示す値も 1234 になる 042 printf(" p1 %d\n", *p1); 043 printf(" p4 %d\n", *p4); 044 // 複数データ(配列と同等) 045 printf("***配列***\n"); 046 int b1[4] = {1, 2, 3, 4}; 047 int b2[4]; 048 for (int i1 = 0; i1 < 4; i1++) 049 b2[i1] = b1[i1]; 050 b2[2] = 200; 051 printf(" b1"); 052 for (int i1 = 0; i1 < 4; i1++) 053 printf(" %d", b1[i1]); 054 printf("\n b2"); 055 for (int i1 = 0; i1 < 4; i1++) 056 printf(" %d", b2[i1]); 057 printf("\n***配列(new)***\n"); 058 int *p2 = new int [4]; 059 for (int i1 = 0; i1 < 4; i1++) 060 p2[i1] = i1 + 1; 061 int *p5 = p2; // p5 = &p2[0] と同じ 062 p5[2] = 200; // *(p5+2) = 200; と同じ 063 printf(" p2"); 064 for (int i1 = 0; i1 < 4; i1++) 065 printf(" %d", p2[i1]); 066 printf("\n p5"); 067 for (int i1 = 0; i1 < 4; i1++) 068 printf(" %d", p5[i1]); 069 // クラスのオブジェクト 070 printf("\n***クラスのオブジェクト***\n"); 071 Complex c1; 072 Complex c2 = c1; 073 c2.x = 10; 074 c2.y[0] = 20; 075 c2.z[0] = 30; 076 printf(" c1 x %d y[0] %d z[0] %d\n", c1.x, c1.y[0], c1.z[0]); 077 printf(" c2 x %d y[0] %d z[0] %d\n", c2.x, c2.y[0], c2.z[0]); 078 printf("***クラスのオブジェクト(new)***\n"); 079 Complex *p3 = new Complex(); 080 Complex *p6 = p3; 081 p6->x = 10; 082 p6->y[0] = 20; 083 p6->z[0] = 30; 084 printf(" p3 x %d y[0] %d z[0] %d\n", p3->x, p3->y[0], p3->z[0]); // (*p3).x,・・・ でも可 085 printf(" p6 x %d y[0] %d z[0] %d\n", p6->x, p6->y[0], p6->z[0]); // (*p6).x,・・・ でも可 086 // STL の vector 087 printf("***STL の vector***\n"); 088 vector <int> d1(4, 0); 089 vector <int> d2 = d1; 090 d2[1] = 10; 091 printf(" d1"); 092 for (int i1 = 0; i1 < 4; i1++) 093 printf(" %d", d1[i1]); 094 printf("\n d2"); 095 for (int i1 = 0; i1 < 4; i1++) 096 printf(" %d", d2[i1]); 097 printf("\n***STL の vector(new)***\n"); 098 vector <int> *p7 = new vector <int> (4, 0); 099 vector <int> *p8 = p7; 100 (*p7)[1] = 10; 101 printf(" p7"); 102 for (int i1 = 0; i1 < 4; i1++) 103 printf(" %d", (*p7)[i1]); 104 printf("\n p8"); 105 for (int i1 = 0; i1 < 4; i1++) 106 printf(" %d", (*p8)[i1]); 107 printf("\n"); 108 // 領域の開放 109 delete p1; 110 delete [] p2; 111 delete p3; 112 delete p7; 113 114 return 0; 115 }
***単純変数*** a1 0 a2 1234
***単純変数(new)*** p1 1234 p4 1234
***配列*** b1 1 2 3 4 b2 1 2 200 4
***配列(new)*** p2 1 2 200 4 p5 1 2 200 4
***クラスのオブジェクト*** c1 x 0 y[0] 0 z[0] 30 c2 x 10 y[0] 20 z[0] 30
***クラスのオブジェクト(new)*** p3 x 10 y[0] 20 z[0] 30 p6 x 10 y[0] 20 z[0] 30
***STL の vector*** d1 0 0 0 0 d2 0 10 0 0 ***STL の vector(new)*** p7 1 10 1 1 p8 1 10 1 1