Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [paho-dev] Using C++ with the Paho C client library

No I wouldn't think to start adding const to specific fields in a structure. Certainly not to indicate which members can be updated by an arbitrary function call.

But why do you us a pointer to pass a struct to a function instead of passing a copy of the whole struct by value? One of two reasons...
(1) You want to change elements of the struct and return those changes to the caller.
(2) As an optimization so that you only push a pointer onto the stack and not the whole struct. But you have no intention of changing anything.

The use (or lack) of "const" tells the user about the intent of the function:
// I am going to change fields in 'response' and return them to you.
int func(MQTTAsync_responseOptions* response);

// I promise not to change anything in 'response'.
// I'm only using a pointer as an optimization.
int func(const MQTTAsync_responseOptions* response);


On 05/17/2013 07:01 AM, Ian Craggs wrote:
Hi Roger,

I don't see why it should be unimportant that a member of a structure is not const while the structure is const.  If the purpose is to help the writer of a function using the data not to inadvertently modify caller data, then they are both important.

Maybe I misunderstand the purpose of the suggestion of adding const to these fields.  What do we hope to achieve by doing so, if not the reason I stated above?  And in that case what principle would we be following in deciding which fields to apply const to, and which not?


On 17/05/13 11:43, Roger Light wrote:
Hi Ian,

what about the elements of the connectOptions, willOptions and SSLOptions
structures, for instance?
Neither SSLOptions nor willOptions are directly exposed in the
external API because they are only used as members of connectOptions.
connectOptions itself can be const because none of its members are
modified. My understanding is that it is not important that, for
example, the username member of connectOptions is not const even
though connectOptions may be. Username is mutable but connectOptions
is not.


paho-dev mailing list

paho-dev mailing list

Back to the top