1
MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);

Could anybody explain what is being done here compared to a more traditional client(mqttNetwork); or something like MQTTNetwork *mqttNetwork = new mqttNetwork();?

What I am also trying to accomplish is to modify that line of code into something that I could initialize similarly to MQTTNetwork *mqttNetwork = new mqttNetwork(); so that i can assign a new object to the pointer from inside a loop, as this code will be used in an embedded application with an infinite loop where the object needs to persist between loop iterations.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • 1
    how is `client(mqttNetwork);` more traditional than `client(mqttNetwork);` ? – 463035818_is_not_an_ai Jun 06 '18 at 07:33
  • because it has the whole `MQTT::Client` part in front of it that i can't figure out the meaning of. – Rasmus Karlsson Jun 06 '18 at 07:39
  • 1
    thats the type of the variable you are declaring, just like `MQTTNetwork*` is the type of `mqttNetwork`. Btw your reasoning to use pointers and manual memory allocations is moot. You dont need a pointer to be able to assign a new object. Drastically simplified, but in principle the same, you also dont need pointers to do something like `int a=3; a = 5;` – 463035818_is_not_an_ai Jun 06 '18 at 07:47
  • Good point about not needing to use a pointer, I guess i'm a bit tired or something. Anyway, this problem is much clearer now thanks to your and KorbenDose's answers. – Rasmus Karlsson Jun 06 '18 at 07:59
  • `client(mqttNetwork);` would be a syntax error (unless `client` is already defined) – M.M Jun 06 '18 at 23:35

2 Answers2

1

Well, the basic difference between the mentioned approach and the pointer approach is that the pointer is allocated on the heap while the object client lies on the stack.

If client is being defined and initialized inside the loop, the destructor will be called after each loop and the object will be created newly each time, anyway.

Since the object needs to persist between loop iterations, client is probably defined outside the loop. In this case you might want to look up if the library you're using provides something as a clear() or reset() function and a way to fill new data into the object. That way you could use the same object all the way and just assign new data.

Note: If you're using the pointer method and if it's possible, I would consider using smart pointers.

KorbenDose
  • 797
  • 1
  • 7
  • 23
  • 1
    Smart pointers seem like something that will be really useful, never even heard about them before. Anyway, could you explain what the `MQTT::Client` part does? Also, due to what I'm trying to do, i need to occasionally create a completely new client object from within the loop if a certain situation happens - The library has no reset or similar function. Thus, I want the pointer to be defined outside the loop so that i can just assign new objects to it. – Rasmus Karlsson Jun 06 '18 at 07:30
  • 1
    I'm not sure how familiar you are with templates, but it might be a good idea to take a look into that (e.g. [here](https://stackoverflow.com/questions/152318/learning-c-templates) oder [here](https://en.cppreference.com/w/cpp/language/class_template)). `MQTT::Client` basically just defines a specific instantiation of a template with the template parameters being `MQTTNetwork` and `Countdown`. – KorbenDose Jun 06 '18 at 07:46
  • 1
    Okay, i wasn't familiar with them at all. That makes it a lot clearer! – Rasmus Karlsson Jun 06 '18 at 07:57
1

To provide an answer for the first question: MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); is conceptionally the same as std::vector<double> numbers(10) or std::unordered_map<int, double> myMap(otherMap.begin(), otherMap.end());

The first part is just the type of the object you want to construct, and it happens to be a template type (just like std::vector), thus requiring you to spell out the template arguments.

Max Langhof
  • 23,383
  • 5
  • 39
  • 72