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
7
votes
2 answers

Constant in objective-c

I would like to add some constant keys for my application, these constants can be accessed anywhere in program. So I declare the constants in interface file: #import NSString * MIN_INTERVAL_KEY = @"MIN_INTERVAL_KEY"; NSString *…
haisergeant
  • 1,111
  • 2
  • 13
  • 32
7
votes
3 answers

Overhead of static constants when included into many translation units?

In a header file, global constants can be declared and (pre)defined in a single line. // constants.h namespace Constant{ static const unsigned int framerate = 60; static const char * const windowName = "Test"; static const unsigned char…
Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
7
votes
8 answers

Where to define constants in Java that are used in a couple of unrelated classes

The question of where to define constants in Java has appeared numerous times in forums, yet I am struggling to settle on a solution I feel comfortable with. To make it simple, suppose I have two classes: WriteMyData and ReadMyData. None is a…
DustByte
  • 651
  • 6
  • 16
7
votes
1 answer

non-const pointer argument to a const double pointer parameter

The const modifier in C++ before star means that using this pointer the value pointed at cannot be changed, while the pointer itself can be made to point something else. In the below void justloadme(const int **ptr) { *ptr = new int[5]; } int…
legends2k
  • 31,634
  • 25
  • 118
  • 222
7
votes
2 answers

Writing powers of 10 as constants compactly

I'm reading the recently released The Go Programming Language, and it's been a joy so far (with Brian Kernighan being one of the authors, I wouldn't expect anything other than excellence anyway). I've come across the following exercise on chapter…
Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
7
votes
3 answers

What is the difference between using initialization lists to initialize fields and initialize them inside the constructor?

On some tutorials (e.g. http://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm) I read that the following two codes are equivalent. First code: class MyClass1{ public: int a; int b; MyClass1(int a, int b)…
gvgramazio
  • 1,115
  • 3
  • 13
  • 30
7
votes
3 answers

Does it make sense to use const for all variables that will never be changed?

Given something like this: const audio = React.findDOMNode(this.refs.audio); const seeker = React.findDOMNode(this.refs.seeker); const {left, right} = seeker.getBoundingClientRect(); const seekToPerc = (event.clientX - left) / (right -…
ffxsam
  • 26,428
  • 32
  • 94
  • 144
7
votes
1 answer

Static const member initialization in templated class

I have a problem regarding 'static const' member initialization. In a templated class I define a const member and initialize it outside the class. When I include the .h file where this class is implemented in multiple .cpp files, I get an LNK2005…
Gratian Lup
  • 1,485
  • 3
  • 19
  • 29
7
votes
1 answer

c++: why can't we convert char ** to const char **

I know that const char * p means we can't change the value which is pointed by p through p. Now I'm writing a function which will take the parameters of the function main. Meaning that this function will take a char **. So I write this function like…
Yves
  • 11,597
  • 17
  • 83
  • 180
7
votes
2 answers

In C++14 can a constexpr member change a data member?

In C++14, since constexpr are not implicitly const anymore, can a constexpr member function modify a data member of a class: struct myclass { int member; constexpr myclass(int input): member(input) {} constexpr void f() {member = 42;} //…
Vincent
  • 57,703
  • 61
  • 205
  • 388
7
votes
2 answers

returning constant object and assigning it to non-constant object

I've found strange behavior of a code which is apparently ignoring const-ness: #include using std::cerr; class A { public: A() { cerr << "A::A()\n"; } A(const A &a) { cerr << "A::A(const A&)\n"; } A(A &&) { cerr <<…
Alexander Sergeyev
  • 922
  • 10
  • 19
7
votes
2 answers

How to dynamically retrieve a constant in java?

I have several interfaces all with the same constants - ID and ROOT. I also have a method into which I pass an object that will be an implementation of one of these interfaces. How can I dynamically retrieve the value of the constant depending on…
Michael Jones
  • 97
  • 3
  • 7
7
votes
4 answers

Why is it allowed in C++ to modify a constant object's pointer member variable's memory from outside?

I've been trying to understand when I write a function in C++ with a constant argument and a pointer variable inside of that object than the const flag is not protecting the underlying memory against modifications. For example it's perfectly legal…
Kristof01
  • 71
  • 1
  • 3
7
votes
4 answers

Is it safe to pass Delphi const string params across memory manager boundaries?

Subj. I'd like to use strings instead of PChar because that spares me much casting, but if I just do procedure SomeExternalProc(s: string); external SOMEDLL_DLL; and then implement it in some other project with non-shared memory manager: library…
himself
  • 4,806
  • 2
  • 27
  • 43
7
votes
3 answers

Passing ECMAScript 6 const as Function Argument

I've been reading a lot about ES6 lately and decied to give it a try (using Babel). I'm a little confused with new variable declarations let and const. I understood how scope differs from var; and that const is a permanent reference to a value (and…
Bugs Bunny
  • 2,496
  • 1
  • 26
  • 32