0

I have a problem with Classes. I have a bunch of code, that code is uses Adafruit MQTT FONA library. I call the Class for make new Instance and Connect to specified server with this code;

Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

When I want to connect to another server, I call this Instance's constructor after it called once. But it don't let me to construct it again. How can I construct the same instance again after it constructed once?

void connectMqtt()
{
    Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
}

connectMqtt();

Thanks for helps! Have a good day!

  • note: your question is not about Arduino .... it is a C++ programming question – jsotola Dec 2 '18 at 18:53
3

You can't. Once constructed, an object remains constructed until you delete it.

You could either use dynamic allocation and new and delete to create and destroy objects at will, or you could create multiple objects with different settings and use different objects at different times.

Another option would be to modify the library to create a new function that accepts and assigns new server settings. All the constructor does is store the settings in variables - there's no reason why you couldn't just change those variables.

Not the answer you're looking for? Browse other questions tagged or ask your own question.