回答編集履歴
1
追記
answer
CHANGED
@@ -39,4 +39,58 @@
|
|
39
39
|
std::cout << line << std::endl;
|
40
40
|
}
|
41
41
|
}
|
42
|
+
```
|
43
|
+
[追記] コンパイル単位を分けてみた:
|
44
|
+
```C++
|
45
|
+
// Module.h
|
46
|
+
#pragma once
|
47
|
+
|
48
|
+
#include <string>
|
49
|
+
|
50
|
+
int LoadModule(const std::string& path);
|
51
|
+
std::string GetLine(int handle);
|
52
|
+
```
|
53
|
+
|
54
|
+
```C++
|
55
|
+
// Module.cpp
|
56
|
+
#include "Module.h"
|
57
|
+
#include <vector>
|
58
|
+
#include <fstream>
|
59
|
+
|
60
|
+
namespace {
|
61
|
+
class Module {
|
62
|
+
std::ifstream stream_;
|
63
|
+
public:
|
64
|
+
Module(const std::string& path) : stream_(path) {}
|
65
|
+
std::string get() { std::string line; std::getline(stream_, line); return line; }
|
66
|
+
};
|
67
|
+
|
68
|
+
std::vector<Module> mods; // Module集合
|
69
|
+
}
|
70
|
+
|
71
|
+
/* Moduleを生成し modsに追加。*/
|
72
|
+
int LoadModule(const std::string& path) {
|
73
|
+
int handle = mods.size();
|
74
|
+
mods.emplace_back(path);
|
75
|
+
return handle;
|
76
|
+
}
|
77
|
+
|
78
|
+
/* Module::get() をcallする */
|
79
|
+
std::string GetLine(int handle) {
|
80
|
+
return mods.at(handle).get();
|
81
|
+
}
|
82
|
+
```
|
83
|
+
|
84
|
+
```C++
|
85
|
+
// foo.cpp
|
86
|
+
#include <iostream>
|
87
|
+
#include "Module.h"
|
88
|
+
|
89
|
+
int main(){
|
90
|
+
int handle = LoadModule("foo.cpp");
|
91
|
+
for ( int i = 0; i < 4; ++i ) {
|
92
|
+
std::string line = GetLine(handle);
|
93
|
+
std::cout << line << std::endl;
|
94
|
+
}
|
95
|
+
}
|
42
96
|
```
|