[Qt-creator] const-correctness and setter

Lorenz Haas lykurg at gmail.com
Fri Jun 6 07:06:16 CEST 2014


Hi,

this questions puzzles me the last days and I hope I do not overlook the
obvious... but when we create a setter (also with the "create getter and
setter" quick fix) we write

void setFoo(int foo);

but not

void setFoo(CONST int foo);

Why? If int would not be a POD, we would pass a const reference mainly
to avoid the costs of passing by value. This, of course, is not
necessary for an integer. The second aspect, however, is that we promise
the caller, we wont change the argument, - which is also not needed if
we pass the argument by value - but it also protects us from
accidentally changing the argument's value. With setFoo(int foo) we
forgo this aspect.

Additionally when we create a local variable in a function, we declare
it const. Again, to protect us from accidentally changing it, but also
that the compiler can put this variable into the read only section to
gain speed/performance etc. Does that not also apply to an argument?


Thanks,
Lorenz



More information about the Qt-creator mailing list