質問編集履歴

1

ソースコードの追加

2016/05/16 03:59

投稿

BeatStar
BeatStar

スコア4958

test CHANGED
File without changes
test CHANGED
@@ -98,7 +98,137 @@
98
98
 
99
99
  呼び出し側 ( main関数等 ) での書き方です。
100
100
 
101
-
101
+ ```C++
102
+
103
+ #include<iostream>
104
+
105
+
106
+
107
+ using namespace std;
108
+
109
+
110
+
111
+ // インターフェースクラス
112
+
113
+ class ITest{
114
+
115
+ public:
116
+
117
+ virtual ~ITest(){}
118
+
119
+ virtual int func() = 0;
120
+
121
+ };
122
+
123
+
124
+
125
+ // 実際に使いたいクラス
126
+
127
+ class CTest : public ITest{
128
+
129
+ private:
130
+
131
+ bool bCheck;
132
+
133
+ protected:
134
+
135
+ public:
136
+
137
+ CTest( bool bCheck = false );
138
+
139
+ ~CTest();
140
+
141
+
142
+
143
+ int func();
144
+
145
+ };
146
+
147
+
148
+
149
+ // 子クラスのコンストラクタ
150
+
151
+ CTest::CTest( bool bCheck ){
152
+
153
+ if( bCheck == false ) throw -1;
154
+
155
+ this->bCheck = bCheck;
156
+
157
+ }
158
+
159
+
160
+
161
+ // 子クラスのデストラクタ
162
+
163
+ CTest::~CTest(){
164
+
165
+ cout << "Destructor" << endl;
166
+
167
+ }
168
+
169
+
170
+
171
+ // 子クラスのメンバ関数
172
+
173
+ int CTest::func(){
174
+
175
+ return 100;
176
+
177
+ }
178
+
179
+
180
+
181
+
182
+
183
+ // FactoryMethod を利用して 生成する関数
184
+
185
+ ITest* CreateInstance( bool bCheck ){
186
+
187
+ return new CTest( bCheck );
188
+
189
+ }
190
+
191
+
192
+
193
+
194
+
195
+ int main( int argc, char *argv[] )
196
+
197
+ {
198
+
199
+ ITest* obj;
200
+
201
+ try{
202
+
203
+ cout << 1 << endl;
204
+
205
+ obj = CreateInstance( false );
206
+
207
+ cout << 2 << endl;
208
+
209
+ }catch(int n){
210
+
211
+ cout << 3 << endl;
212
+
213
+ delete obj; // ここで実行時エラー.
214
+
215
+ cout << 4 << endl;
216
+
217
+ }
218
+
219
+
220
+
221
+ // 停止用
222
+
223
+ getchar();getchar();
224
+
225
+ return 0;
226
+
227
+ }
228
+
229
+
230
+
231
+ ```
102
232
 
103
233
  [環境等]
104
234