0

I read a lot article from website, it said that const help to understand the source better because you have an insurance that the value is fixed. Is this the only reason using const?

Thunder
  • 89
  • 1
  • 1
  • 7
  • 2
    Read the duplicate topic. [My answer addresses your question](http://stackoverflow.com/a/19478103/415784). – Nawaz Dec 07 '13 at 08:26
  • 2
    And never underestimate the power of giving the optimizer as much up-front ammunition as you can for a well-done job. It isn't just good practice, its *beneficial* in multiple way. The above link is pretty solid, btw. – WhozCraig Dec 07 '13 at 08:34
  • Oh okay.. :D Can I ask what does it mean by const is viral? means if one uses it once it must be used everywhere? but why? Sorry for my ignorance, I am new to c++. – Thunder Dec 07 '13 at 08:40

2 Answers2

0

By using const you are using the compiler to spot when you are trying to write to something that you shouldn't. I think that having the compiler tell me that I am doing something wrong is always good. Also many compilers will optimise more efficiently when given the information that something in not ever going to change.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
-1

It's a good reason, but another is that when you use references wheither they are const or not makes a big difference. For instance

void f(string& ref);
void g(const string& ref);
string h();

f(h()); // illegal code
g(h()); // legal code

The difference is because of the rule you cannot bind a temporary to a non-const reference. The return value of h is a temporary, and so only the call to g is legal. If you have f you would need to use an additional variable

string tmp = h();
f(tmp);
john
  • 85,011
  • 4
  • 57
  • 81
  • I don't think it answers the question, What if one writes `void f(string && ref);`? Do you know `f(h());` would be legal then? – Nawaz Dec 07 '13 at 08:28
  • @Nawaz No I didn't, still seems like a reasonable answer though. – john Dec 07 '13 at 08:32
  • It is not reasonable. If `g()` needs to store `ref` in an internal storage, then a copy would be created which is unnecessary if you pass `&&` which can be *moved*. Again, you've third (better) option, pass by value, then move the object to the internal storage. – Nawaz Dec 07 '13 at 08:40
  • Sorry but the purpose of the answer was not to illustrate best coding practice. – john Dec 07 '13 at 08:51
  • So what does your answer illustrate? That "how NOT to code"? – Nawaz Dec 07 '13 at 08:59
  • Can I ask what does it mean by const is viral? means if one uses it once it must be used everywhere? but why? Sorry for my ignorance, I am new to c++. – Thunder Dec 07 '13 at 09:02
  • @Nawaz Now I'm in a bad mood, why don't you just go away, you're not contributing anything. – john Dec 07 '13 at 09:13
  • -1 for "contributing" a BAD answer. I mean, seriously, this is a very bad answer. If you edit and improve it, then I will remove the downvote, though. – Nawaz Dec 07 '13 at 09:15
  • @Nawaz Tell you what, I was about the answer Thunders question about why const is viral, but why don't you take the oppotunity to answer a question. Maybe it will help the OP. – john Dec 07 '13 at 09:16
  • @john: I've already done that in the other topic. See the link to the duplicate. – Nawaz Dec 07 '13 at 09:17
  • @john: Why? Because I've answered the question? – Nawaz Dec 07 '13 at 09:19
  • Chill Chill.. :) @Nawaz [this?](http://stackoverflow.com/questions/19478034/why-arent-c-types-const-by-default/19478103#19478103) – Thunder Dec 07 '13 at 09:23
  • Nawaz Thanks.. :D @john Can I have your answer for my question too? :) – Thunder Dec 07 '13 at 09:37
  • @john John? Can I have your answer for my question too? :) – Thunder Dec 07 '13 at 11:03