c++ - 如何创建 "weak"函数并将其用于对象?

标签 c++ class weak

我正在编写一个名为“MQTT_interface”的类。我需要在此类的“重新连接”方法中使用“回调”功能。我想开发人员会为自己编写函数“回调”,如果没有,将会有一种同名的弱函数(“回调”),它会向控制台发送错误。

我不知道这是否可能。

我有这些文件:

MQTT_interface.h

class MQTT_interface : public PubSubClient
{
    public:
    MQTT_interface(Client& c, String hostname, uint16_t port = 1883);
    void reconnect(void);

    private:
    uint8_t buf[UART_MAX_TRANSFER_SIZE];
};

MQTT_interface.cpp

void MQTT_interface::reconnect() {

    while (!connected()) {
  set_server(MQTT_auth_data.mqtt0_server, MQTT_auth_data.mqtt_port);
    if (connect(MQTT::Connect(MQTT_auth_data.mqtt_uid)
                           .set_auth(MQTT_auth_data.mqtt_uid, MQTT_auth_data.mqtt_pass))) {
      Serial.println("Connected to MQTT server");
      set_callback(callback);   //here my function must be used
        subscribe(topics.mqtt_subscribe_topic); // this is our receive filter. We able to receive only these topics
        subscribe(topics.mqtt_topic_1);

      } else {
        delay(timeout);
    }
  }
}

main.cpp

// Callback function
//  Receive data from MQTT and send it to serial
uint8_t buf[UART_MAX_TRANSFER_SIZE];
void callback(const MQTT::Publish& pub) {
// Copy the payload to a new message
  //MQTT::Publish newpub("outTopic", pub.payload(), pub.payload_len());
  //client.publish(newpub);
  String start_symbol = "";
  char end_symbol = '\n';
  String div_symbol = "&";
  //Serial.println(pub.topic());
  if (pub.has_stream()) {
    int read;
    while (read = pub.payload_stream()->read(buf, UART_MAX_TRANSFER_SIZE)) {
      Serial.write(buf, read);    
    }
    pub.payload_stream()->stop();
  }

  else{
    char symbol = 1;

    String t = pub.topic();
    String m = pub.payload_string();
    String message = start_symbol + t + div_symbol + m + end_symbol;
    int str_len ;
      str_len = message.length();
      Serial.print(message);
  }
}

最佳答案

让用户给你回调函数:

class MQTT_interface
{
public:
    using reconnect_callback = void(const MQTT::Publish&);

    static void reconnect_callback_default(const MQTT::Publish&) { /* your impl */ }
    void reconnect(reconnect_callback callback = reconnect_callback_default)
    {
        callback(/*...*/);
    }
};

Live complete example :

#include <iostream>

struct example
{
    using reconnect_callback = void(int);
    static void reconnect_callback_default(int) { std::cout << "Error\n"; }
    void reconnect(reconnect_callback callback = reconnect_callback_default)
    {
        callback(42);
    }
};

int main()
{
    example e;
    e.reconnect();                                     // Error
    e.reconnect([](int n){ std::cout << n << '\n'; }); // 42
}

这可以通过多种方式进行改进。例如,Max Langhof 建议将 reconnect_callback 定义为 std::function,这样它就可以成为有状态的仿函数。

关于c++ - 如何创建 "weak"函数并将其用于对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54129154/

相关文章:

c++ - 如何使模板函数成为模板类的 friend

c++ - 从内存映射寄存器读取

python - 用户定义的类是可变的

java - 我无法在 Eclipse 上给一个类命名

c++ - 是否有 boost::weak_intrusive_pointer?

c++ - 如何修复 C++ 中的 'arrStud was not declared in this scope' 错误

c++ - c++ matlab代码上的奇怪错误

Ruby - 如何在不多次编写的情况下为多种方法引发相同的错误?

boost - 基于分布的弱学习器 : Decision stump

c - GCC 变量声明中的弱属性