小编sti*_*ker的帖子

C++ 中的 ()() 运算符是什么?

有什么void operator()()作用?在尝试用 python 重写 C++ 程序时发现了这个,但甚至无法理解这个方法的功能。它不是从代码中的任何地方调用的,但无论如何它都会被程序调用并且无法真正理解这是怎么回事?程序本身什么时候调用这样的东西?

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) …
Run Code Online (Sandbox Code Playgroud)

c++ python operator-overloading operator-keyword

3
推荐指数
1
解决办法
199
查看次数