{
class MoistureSensor {
const std::chrono::seconds sleepTime;
std::mutex& mtx;
std::set<WaterDevice*> devices;
const int min = 0;
const int max = 10;
const int threshold = 3;
public:
MoistureSensor(const std::chrono::seconds sleepTime, std::mutex& mtx)
: sleepTime{ sleepTime }
, mtx{ mtx }
{
}
void subscribe(WaterDevice& device) {
devices.insert(&device);
}
void operator()(){
for (;;) {
std::cout << "this\n";
std::unique_lock<std::mutex> lock(mtx);
if (isAirTooDry())
for (auto p : devices)
p->sprinkleWater();
if (isSoilTooDry())
for (auto p : devices)
p->pourWater();
lock.unlock();
std::this_thread::sleep_for(sleepTime);
}
}
void foo();
private:
bool isAirTooDry();
bool isSoilTooDry();
int getAirMoisture();
int getSoilMoisture();
};
Python中是否有类似的东西
它是一个不接受任何参数且没有返回值的函子。将第一个
()
视为运算符的名称,第二个()
表示参数例如,如果
Foo
有这样一个操作符,并且foo
是Foo
的实例,那么将调用
void operator()()
如果您想将
foo
的构造与其调用分开,那么它很有用。在很多方面,这是C++中lambda函数的先导,特别是在完全可以在函数范围内声明和定义类型的情况下。p>相关问题 更多 >
编程相关推荐