C++ class 在构造函数中实例化对象时的成员作用域
C++ class member scope when instatiated object in constructor
我在 class 构造函数中有这段代码:
MqttSensorInterface::MqttSensorInterface(Client& client, String sensorTopic)
{
this->mqttClient = PubSubClient(client);
this->sensorTopic = sensorTopic;
this->askMeasureTopic = sensorTopic + "/askmeasure";
this->publishMeasureTopic = sensorTopic + "/measure";
}
但是在创建新的MqttSensorInterface
对象时使用构造函数后,在构造函数中实例化的PubSubClient
对象被析构(调用PubSubClient
析构函数)。我是 C++ 的新手,不知道这段代码是否有问题。由于 PubSubClient
对象在构造函数中被实例化,但是 class 成员 mqttClient
被设置为这个对象,它的作用域是什么?
PubSubClient 构造函数代码:
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);
}
编辑
通过这种方式使用成员初始化列表解决:
MqttSensorInterface::MqttSensorInterface( Client& client, String sensorTopic): mqttClient(client)
当构造函数的主体
MqttSensorInterface::MqttSensorInterface(String sensorTopic)
{
WiFiClient wiFiClient;
this->mqttClient = PubSubClient(wifiClient);
this->sensorTopic = sensorTopic;
this->askMeasureTopic = sensorTopic + "/askmeasure";
this->publishMeasureTopic = sensorTopic + "/measure";
}
获取数据成员 mqttClient 的控件已经使用默认构造函数创建 PubSubClient
前提是在 class 定义中没有数据成员的显式初始化程序,
所以在这个语句的正文里面
this->mqttClient = PubSubClient(wifiClient);
通过显式调用构造函数 PubSubClient(wifiClient)
创建了一个 PubSubClient
类型的临时对象,并且使用以下任一方法将该临时对象分配给数据成员 this->mqttClient
复制赋值运算符或移动赋值运算符。在语句执行结束时,临时对象被销毁。
如果可能的话,可以在构造数据成员的过程中在构造函数的内存初始化器列表中初始化数据成员。
我在 class 构造函数中有这段代码:
MqttSensorInterface::MqttSensorInterface(Client& client, String sensorTopic)
{
this->mqttClient = PubSubClient(client);
this->sensorTopic = sensorTopic;
this->askMeasureTopic = sensorTopic + "/askmeasure";
this->publishMeasureTopic = sensorTopic + "/measure";
}
但是在创建新的MqttSensorInterface
对象时使用构造函数后,在构造函数中实例化的PubSubClient
对象被析构(调用PubSubClient
析构函数)。我是 C++ 的新手,不知道这段代码是否有问题。由于 PubSubClient
对象在构造函数中被实例化,但是 class 成员 mqttClient
被设置为这个对象,它的作用域是什么?
PubSubClient 构造函数代码:
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);
}
编辑
通过这种方式使用成员初始化列表解决:
MqttSensorInterface::MqttSensorInterface( Client& client, String sensorTopic): mqttClient(client)
当构造函数的主体
MqttSensorInterface::MqttSensorInterface(String sensorTopic)
{
WiFiClient wiFiClient;
this->mqttClient = PubSubClient(wifiClient);
this->sensorTopic = sensorTopic;
this->askMeasureTopic = sensorTopic + "/askmeasure";
this->publishMeasureTopic = sensorTopic + "/measure";
}
获取数据成员 mqttClient 的控件已经使用默认构造函数创建 PubSubClient
前提是在 class 定义中没有数据成员的显式初始化程序,
所以在这个语句的正文里面
this->mqttClient = PubSubClient(wifiClient);
通过显式调用构造函数 PubSubClient(wifiClient)
创建了一个 PubSubClient
类型的临时对象,并且使用以下任一方法将该临时对象分配给数据成员 this->mqttClient
复制赋值运算符或移动赋值运算符。在语句执行结束时,临时对象被销毁。
如果可能的话,可以在构造数据成员的过程中在构造函数的内存初始化器列表中初始化数据成员。