I have this code in class constructor:
MqttSensorInterface::MqttSensorInterface(Client& client, String sensorTopic)
{
this->mqttClient = PubSubClient(client);
this->sensorTopic = sensorTopic;
this->askMeasureTopic = sensorTopic + "/askmeasure";
this->publishMeasureTopic = sensorTopic + "/measure";
}
But just after the constructor is used when a new MqttSensorInterface
object is created, the PubSubClient
object instantiated in the constructor is destructed (PubSubClient
destructor is called). I am new with C++ and dont know if there is something wrong with this code. As the PubSubClient
object is instantiated in constructor but the class member mqttClient
is set to be this object, which is its scope?
PubSubClient constructor code:
PubSubClient::PubSubClient(Client& client) {
this->_state = MQTT_DISCONNECTED;
setClient(client);
this->stream = NULL;
this->bufferSize = 0;
setBufferSize(MQTT_MAX_PACKET_SIZE);
setKeepAlive(MQTT_KEEPALIVE);
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
EDIT
Solved by using member initializer list this way:
MqttSensorInterface::MqttSensorInterface( Client& client, String sensorTopic): mqttClient(client)