Questions tagged [constants]

Constants in programming are definitions whose value is fixed throughout a program's execution. Literals in most languages are constants, for example. In referentially transparent programming styles, all definitions are constant. A const-qualified data storage area (object, field, variable, parameter) is one that "never changes", thus allowing extra code generator optimizations and additional static checking of program correctness.

In computer science, const-correctness is the form of program correctness that deals with the proper declaration of objects as mutable or immutable. The term is mostly used in a C or C++ context, and takes its name from the const keyword in those languages.

The idea of const-ness does not imply that the variable as it is stored in the computer's memory is unwriteable. Rather, const-ness is a compile-time construct that indicates what a programmer may do, not necessarily what they can do.

In addition, a method can be declared as const. In this case, the this pointer inside such a method is of const ThisClass* const type rather than of ThisClass* const type. This means that non-const methods for this object cannot be called from inside such a method, nor can member variables be modified. In C++, a member variable can be declared as mutable, indicating that this restriction does not apply to it. In some cases, this can be useful, for example with caching, reference counting, and data synchronization. In these cases, the logical meaning (state) of the object is unchanged, but the object is not physically constant since its bitwise representation may change.

The C++11 standard adds additional notes on the meaning of const when used in the Standard Library with respect to thread access and possible modifications of those const objects.

More about const-correctness here.

10286 questions
162
votes
13 answers

What is the use of interface constants?

I am learning Java and just found that the Interface can have fields, which are public static and final. I haven't seen any examples of these so far. What are some of the use cases of these Interface Constants and can I see some in the Java Standard…
unj2
  • 52,135
  • 87
  • 247
  • 375
162
votes
2 answers

What is the best way to create constants in Objective-C

I am creating a Reddit client for learning purposes. I need to have a file with constants in it. I was thinking about importing the file in the Reddit-Prefix.pch file to make the constants available to all the files. Is it a good way of doing…
Robert Audi
  • 8,019
  • 9
  • 45
  • 67
161
votes
12 answers

Can I get CONST's defined on a PHP class?

I have several CONST's defined on some classes, and want to get a list of them. For example: class Profile { const LABEL_FIRST_NAME = "First Name"; const LABEL_LAST_NAME = "Last Name"; const LABEL_COMPANY_NAME = "Company"; } Is there…
Brock Boland
  • 15,870
  • 11
  • 35
  • 36
161
votes
12 answers

What's the best practice to keep all the constants in Flutter?

What's the best programming practice to create a constant class in Flutter to keep all the application constants for easy reference? I know that there is const keyword in Dart for creating constant fields, but is it okay to use static along with…
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
161
votes
11 answers

Java's final vs. C++'s const

The Java for C++ programmers tutorial says that (highlight is my own): The keyword final is roughly equivalent to const in C++ What does "roughly" mean in this context? Aren't they exactly the same? What are the differences, if any?
WinWin
  • 7,493
  • 10
  • 44
  • 53
160
votes
1 answer

JavaScript (ES6) const with curly braces

I'm new to ECMAScript 6, and while trying to learn Ember, I've seen the following code style occasionally: const { abc, def } = Object; I've searched Google and many sites explaining the new ES6 specifications. I know this is not the current…
tralston
  • 2,825
  • 3
  • 19
  • 22
159
votes
11 answers

Creating a constant Dictionary in C#

What is the most efficient way to create a constant (never changes at runtime) mapping of strings to ints? I've tried using a const Dictionary, but that didn't work out. I could implement a immutable wrapper with appropriate semantics, but that…
David Schmitt
  • 58,259
  • 26
  • 121
  • 165
157
votes
17 answers

What's the point of const pointers?

I'm not talking about pointers to const values, but const pointers themselves. I'm learning C and C++ beyond the very basic stuff and just until today I realized that pointers are passed by value to functions, which makes sense. This means that…
R. Ruiz.
  • 1,673
  • 3
  • 12
  • 11
156
votes
8 answers

Elegant solution to duplicate, const and non-const, getters?

Don't you hate it when you have class Foobar { public: Something& getSomething(int index) { // big, non-trivial chunk of code... return something; } const Something& getSomething(int index) const { // big,…
Michael
  • 2,826
  • 4
  • 25
  • 18
156
votes
4 answers

Difference between `const shared_ptr` and `shared_ptr`?

I'm writing an accessor method for a shared pointer in C++ that goes something like this: class Foo { public: return_type getBar() const { return m_bar; } private: boost::shared_ptr m_bar; } So to support the const-ness of…
Dave Lillethun
  • 2,978
  • 3
  • 20
  • 24
155
votes
8 answers

Why is there no Constant feature in Java?

I was trying to identify the reason behind constants in Java I have learned that Java allows us to declare constants by using final keyword. My question is why didn't Java introduce a Constant (const) feature. Since many people say it has come from…
gmhk
  • 15,598
  • 27
  • 89
  • 112
154
votes
11 answers

constant pointer vs pointer on a constant value

What is the difference between the following declarations? char * const a; const char * a; In order to understand the difference I wrote this small program: #include #include int main (int argc, char **argv) { char a =…
rahmu
  • 5,708
  • 9
  • 39
  • 57
151
votes
4 answers

How many and which are the uses of "const" in C++?

As a novice C++ programmer there are some constructs that look still very obscure to me, one of these is const. You can use it in so many places and with so many different effects that is nearly impossible for a beginner to come out alive. Will some…
tunnuz
  • 23,338
  • 31
  • 90
  • 128
149
votes
5 answers

Why can't I initialize non-const static member or static array in class?

Why can't I initialize non-const static member or static array in a class? class A { static const int a = 3; static int b = 3; static const int c[2] = { 1, 2 }; static int d[2] = { 1, 2 }; }; int main() { A a; return…
Yishu Fang
  • 9,448
  • 21
  • 65
  • 102
149
votes
17 answers

How do I turn off PHP Notices?

Notice: Constant DIR_FS_CATALOG already defined I've already commented out display_errors in php.ini, but is not working. How do I make PHP to not output such things to browsers? UPDATE I put display_errors = Off there but it's still reporting such…
user198729
  • 61,774
  • 108
  • 250
  • 348