我目前正在从事大型代码项目,并希望借此机会学习和使用名称空间。我定义的所有类都驻留在单个名称空间Test中。
我的一个类(在这里称为Thing)具有唯一的ID。我需要能够保存对某些事物的引用的std :: vector,因此我正在使用std :: reference_wrappers。在程序中的某些点,我需要从向量中删除某些std :: reference_wrappers,所以我使用std :: find:
#include <algorithm>
#include <functional>
#include <vector>
namespace Test {
class Thing {
private:
const int id;
public:
Thing(int id);
const int ID() const;
};
}
Test::Thing::Thing(int id) : id(id) { }
const int Test::Thing::ID() const {
return id;
}
inline bool operator==(const Test::Thing& lhs, const Test::Thing& rhs) {
return lhs.ID() == rhs.ID();
}
inline bool operator!=(const Test::Thing& lhs, const Test::Thing& rhs) {
return !(lhs == rhs);
}
int main() { …Run Code Online (Sandbox Code Playgroud)