【问题标题】:How to pass a method to a class if you don't know the exact class of the method being passed如果您不知道所传递方法的确切类,如何将方法传递给类
【发布时间】:2015-02-10 03:06:27
【问题描述】:

对于不知道如何描述我的问题,我深表歉意。我希望这个例子会很清楚。

//My Arduino based device has a real time clock for which I use a library
#include <RTClib.h> 
RTC_DS3234  RTClock(PIN_CLOCK_CS); // uses SPI
DateTime dt = RTClock.now();
// I also have written a logging class which is a singleton
#include <Logger_SD.h>
Logger_SD::Instance()->initializeSD(PIN_SD_CS,PIN_CLOCK_CS,&dt);

所以我的记录器类现在在生成日志消息时使用一个指向 dt 的指针作为它的日期。不幸的是,每次我调用记录器时,我都必须更新 dt 以使其具有正确的日期和时间。

dt = RTClock.now();
Logger_SD::Instance()->msgL(INFO,F("Setting Up MPU6050 Gyro."));

我想传递 RTClock.now() 方法,该方法总是返回一个 DateTime 给 Logger_SD,这样它就可以得到它自己该死的时间。不幸的是,RTC 有很多种。我本来可以做到的:

RTC_DS1307 RTClock(0x68); // Uses i2c
// or
RTC_DS3231 RTClock(0x68); // Also I2c, but does more.
// Lots more.
// then again...
DateTime dt;
dt = RTClock.now();
// I'd like to do something like:
DateTime myNow() return RTClock.now();
Logger_SD::Instance()->initializeSD(PIN_SD_CS,PIN_CLOCK_CS,&myNow);

这些都有一个 now() 方法,它返回一个 DateTime,但是我如何将 now() 方法传递给 Logger_SD 而不必为每个 RTC 对象类都有一个特殊情况?

如果有帮助的话,我的 Logger_SD.h 看起来有点像这样:

class Logger_SD {
    public: // I don't quite understand the mechanics here, but it works great.
        static Logger_SD* Instance(); 
        bool initializeSD(const uint8_t, const uint8_t disable_chip_select,DateTime* dt);
        void msgL(LOG_LEVEL logLevel,const __FlashStringHelper *format, ... );
        // ...
    private:
        Logger_SD(){};
        Logger_SD(Logger_SD const&){};
        Logger_SD& operator=(Logger_SD const&){};
        static Logger_SD* m_pInstance;
        DateTime    *_dt;
        // ...
};

我希望这已经很清楚了。

这是一个简单的例子:

class RTC1 {
  public:
    char  now() { return 'A';}
};
class RTC2 {
  public:
    char  now() { return 'B';}
};
class Logger {
  public:
    void ini(char f()) { logTime = &f ;} // this doesn't work
    char (*logTime)();
};
void setup(){
  Serial.begin(57600);
  RTC1 myRTC1;
  Logger myLogger;
  myLogger.ini(&myRTC1.now);
  Serial.println(MyLogger.logTime()); // Should print 'A'
  // now on a diffent kind of RTC for the same Logger
  RTC2 myRTC2;
  myLogger.ini(&myRTC2.now);
  Serial.println(MyLogger.logTime()); // Should print 'B'
}

【问题讨论】:

    标签: c++ function class pointers arduino


    【解决方案1】:

    myRTC1.now 不能转换为函数指针——它是一个类方法。你需要一个myRTC1 的实例来调用now。你试图按原样做的事情是不可能的。

    你可以做的是采取std::function

    class Logger {
    public:
        using LogFunc = std::function<char()>; // any function that takes no 
                                              // args and returns a char
    
        template <typename F>
        void ini(F&& f) { logTime = std::forward<F>(f); }
        LogFunc logTime;
    };
    

    然后你可以这样分配:

    Logger myLogger;
    myLogger.ini(std::bind(&RTC1::now, myRTC1));
    myLogger.ini([&]{ return myRTC2.now(); });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 2017-01-07
      相关资源
      最近更新 更多