回答編集履歴
1
追記
test
CHANGED
@@ -81,3 +81,111 @@
|
|
81
81
|
}
|
82
82
|
|
83
83
|
```
|
84
|
+
|
85
|
+
[追記] コンパイル単位を分けてみた:
|
86
|
+
|
87
|
+
```C++
|
88
|
+
|
89
|
+
// Module.h
|
90
|
+
|
91
|
+
#pragma once
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
#include <string>
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
int LoadModule(const std::string& path);
|
100
|
+
|
101
|
+
std::string GetLine(int handle);
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
```C++
|
108
|
+
|
109
|
+
// Module.cpp
|
110
|
+
|
111
|
+
#include "Module.h"
|
112
|
+
|
113
|
+
#include <vector>
|
114
|
+
|
115
|
+
#include <fstream>
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
namespace {
|
120
|
+
|
121
|
+
class Module {
|
122
|
+
|
123
|
+
std::ifstream stream_;
|
124
|
+
|
125
|
+
public:
|
126
|
+
|
127
|
+
Module(const std::string& path) : stream_(path) {}
|
128
|
+
|
129
|
+
std::string get() { std::string line; std::getline(stream_, line); return line; }
|
130
|
+
|
131
|
+
};
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
std::vector<Module> mods; // Module集合
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
/* Moduleを生成し modsに追加。*/
|
142
|
+
|
143
|
+
int LoadModule(const std::string& path) {
|
144
|
+
|
145
|
+
int handle = mods.size();
|
146
|
+
|
147
|
+
mods.emplace_back(path);
|
148
|
+
|
149
|
+
return handle;
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
/* Module::get() をcallする */
|
156
|
+
|
157
|
+
std::string GetLine(int handle) {
|
158
|
+
|
159
|
+
return mods.at(handle).get();
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
```
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
```C++
|
168
|
+
|
169
|
+
// foo.cpp
|
170
|
+
|
171
|
+
#include <iostream>
|
172
|
+
|
173
|
+
#include "Module.h"
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
int main(){
|
178
|
+
|
179
|
+
int handle = LoadModule("foo.cpp");
|
180
|
+
|
181
|
+
for ( int i = 0; i < 4; ++i ) {
|
182
|
+
|
183
|
+
std::string line = GetLine(handle);
|
184
|
+
|
185
|
+
std::cout << line << std::endl;
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
```
|