-3

Possible Duplicate:
Sell me on using const correctness

I'm eager to know the answer. [to "What is the benefit of const keyword in programming?"]

Community
  • 1
  • 1
Seetha
  • 51
  • 1
  • 2
  • 2
    why is this getting close-votes? the question is quite clear, albeit in the title – David Hedlund Jul 16 '10 at 09:27
  • 1
    @David I agree, this question isn't that vague. In fact, it's so clear, it's answered by the first Google result for "What is the usage of Const keyword in programming?" – Jason Hall Jul 16 '10 at 09:28
  • 1
    Most of us are happy to be helpful, but... next you'll be asking about the use of `struct` or `#ifdef`. Why not read a book about C programming? – Carl Smotricz Jul 16 '10 at 09:29
  • This is the type of question where a simple google search would get you more information faster - and less downvotes :-) – Péter Török Jul 16 '10 at 09:29
  • @Jason Hall: well yes, i'm no fan of SO being used as the *first* way to solve a problem, either. if there was a close option called RTFG i could understand the votes =) – David Hedlund Jul 16 '10 at 09:30
  • 5
    I believe the community is way too harsh on the OP here. He asked specifically about the *benefit* of const, not its meaning. Technically, every code that contains const modifiers would function identically if they were to be removed, so this is a very valid (albeit novice) question. By and large, the SO community seems very trigger-happy in closing questions these days. – Ofek Shilon Jul 16 '10 at 09:49
  • 1
    @Ofek Shilon: well I'm one of those who've voted to repoen this question, because I thought the original question came through quite clearly, and immediately provoked an accurate response, but to be fair, the original question was indeed about the *use* of `const`; *benefit* was a post-close edit. – David Hedlund Jul 16 '10 at 09:56
  • The box below isn't accurate as well imo, this question isn't 'ambiguous, vague, incomplete, or rhetorical' at all. At the very least, we should have a different wording of the reason for closure, but I guess that's a comment for meta stackoverflow – CalumMcCall Jul 16 '10 at 10:01
  • @teflon19: yes, there were no good options to pick for closing this question, which I think *should* have been enough for alarm bells to go off that perhaps this is a valid question after all. i hope it gets reopened. – David Hedlund Jul 16 '10 at 10:05
  • It's a noob question that could easily be answered with a quick Google search, but it's still a valid question, so I'm voting to re-open. – Paul R Jul 16 '10 at 11:09
  • 1
    http://stackoverflow.com/questions/136880/sell-me-on-using-const-correctness – JohnMcG Jul 16 '10 at 15:27

4 Answers4

9

const indicates that the value assigned to the variable cannot change. If you try to change the value you should get a compiler error.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
1

The const keyword can declare a read only variable.

Using const parameters to a method tells you the method will not change the parameter.

A const method tells you that the method will not alter a class's member variables (but can change member variables marked as mutable)

You can also declare const pointers, better described here

John Warlow
  • 2,922
  • 1
  • 34
  • 49
1

What is the benefit of const keyword in programming?

Specifying a variable as const states that the variable's value should never change after the initial assignment. This allows the compiler to perform additional tests at compilation (validating your code).

For example, if a const function changes a (non-mutable) member in an object, the compiler will yield an error.

utnapistim
  • 26,809
  • 3
  • 46
  • 82
0

Benefit: You get more compile time checks to ensure that you're not changing data that shouldn't be changed.

Cost: You have to use it everywhere. If you need to, you can cast your way out of it, nullifying the benefits.

Getting usage right can be tricky with pointers. Is the pointer itself const, or the data it refers to? This is also the most common usage I've seen: you want to point to immutable memory.

nmichaels
  • 49,466
  • 12
  • 107
  • 135