Public variables

Hi.
In this piece of code there are some Public Variables like this:

/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------Public variables-------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
WiFiClient          g_WiFiClient;
PubSubClient        g_mqttPubSub(g_WiFiClient);

Then, later in the code, there is anything like this:

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // MQTT Init
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    g_mqttPubSub.setServer(g_mqtt_server, g_mqttPort);
    g_mqttPubSub.setCallback(MqttReceiverCallback);

It seems to me that, in this way, the code's creator , using the public variables, saving himselt to write only:

PubSubClient(WiFiClient).setServer(g_mqtt_server, g_mqttPort)
PubSubClient(WiFiClient).setCallback(MqttReceiverCallback);

and nothing else.
Is this right or are there other advantages that I don't see ?
Any idea?
Thanks in advance.

which piece of code ?

I suppose you mean global variables not "public"

probably not. first the syntax is likely wrong for C++ and if you had it correctly and inside a function, the instance would be scoped to the function and be deleted the minute you leave the function. Global variables will have a lifetime matching the duration of the code

I think there is a difference between

  • Buy a new phone
  • Keep it on the table
  • Dial a number
  • Talk into it

and

  • Buy a new phone and dial a number
  • Throw it away
  • Buy a new phone and talk into it
  • Throw it away
3 Likes

Nothing similar.
The PubSubClient and WiFiClient are the data types, while the g_WiFiClient and g_mqttPubSub are the variables of these types. It is a completely different things.

As an example, below is a code, illustrates working with variables:

int x;
int y;
int z;

z = x + y;

and what you suggested to write instead it:

(int) = (int) + (int);

It is not the same as first code, this code won't compile and, in general, it is complete non-sense.

what you said would have made sense if those were #define, ie textual substitution at compile time. but here it's not just that. It's two real variables being allocated and then used

Please indicate to whom you wrote your comment...

@b707

ah weird - I did click reply on the first post from @m4biz usually it would have a link to that post - may be it does not work when it's the first one?

@b707

ah weird - I did click reply on the first post from @m4biz usually it would have a link to that post - may be it does not work when it's the first one?

(testing with replying to you)


so when clicking reply (under the post) within the thread it seems to work but not for the original post... weird

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.