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
4 answers

What is the point of non-static const data member?

For the following code: class A { public: const int cx = 5; }; Here, an instance of cx will be created for every object of A. Which seems a waste to me, as cx can never be modified. Actually, I don't see any reason why compiler shouldn't…
Sam
  • 173
  • 1
  • 7
7
votes
3 answers

Should a method that waits for a change of state be const?

In a multithreaded scenario, I have a method like this: bool WaitForChange( time_duration WaitTime ) const; This method waits either until the state of the object has changed and returns true, or until the timeout times out (how do you say that?)…
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
7
votes
3 answers

Expose the const and nonconst versions of begin() and end() to iterate member vector with smart pointer

// Cat.h class Cat {public: void const_meow() const{ ... }; void meow(){ ... }; }; class CatLibrary {public: std::vector>::iterator begin() { return m_cat_list.begin(); } // compile error,…
Chen OT
  • 3,486
  • 2
  • 24
  • 46
7
votes
3 answers

To copy a vector of unique pointer into new vector

When I compile the below code, I get a compilation error: std::vector> tmpVec; for(const auto& it: hrzBoxTmpMap){ for(const auto& it2: hrzBoxVec){ std::copy_if(hrzBoxVec.begin(), hrzBoxVec.end(), tmpVec.begin(),…
user3665224
  • 1,349
  • 3
  • 16
  • 34
7
votes
4 answers

C++: Why does gcc prefer non-const over const when accessing operator[]?

This question might be more appropriately asked regarding C++ in general, but as I am using gcc on linux that's the context. Consider the following program: #include #include #include using namespace std; template…
JonasW
  • 182
  • 1
  • 4
7
votes
1 answer

Is there any gain in Swift by defining constants instead of variables as much as possible?

Is there any gain in speed, memory usage, whatever, in Swift by defining as much as possible constants x vars? I mean, defining as much as possible with let instead of var?
Duck
  • 34,902
  • 47
  • 248
  • 470
7
votes
4 answers

How to provide stl like container with public const iterator and private non-const iterator?

I have a class that includes a std::list and wish to provide public begin() and end() for const_iterator and private begin() and end() for just plain iterator. However, the compiler is seeing the private version and complaining that it is private…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
7
votes
2 answers

PHP, SPL predefined constants

where can i get some references about SPL predefined constants like SELF_FIRST,CHILD_FIRST ? on php.net i don't get much(just their type).
kmunky
  • 15,383
  • 17
  • 53
  • 64
7
votes
1 answer

Why does -Wunused-variable in GCC produce an error even on static const?

I have a header, core/types.hh, used by several different build targets. It has the following declaration: core/types.hh typedef std::size_t Size; static const Size SZ_MAX = std::numeric_limits::max(); ... Some of the targets use this…
Yuval
  • 3,207
  • 32
  • 45
7
votes
2 answers

How to group Windows API constants

When defining Windows API const values, is it better to have them as const public const int SW_HIDE = 0; public const int SW_SHOWNORMAL = 1; public const int SW_NORMAL = 1; public const int SW_SHOWMINIMIZED = 2; public const int SW_SHOWMAXIMIZED =…
JDMX
  • 1,489
  • 9
  • 22
7
votes
5 answers

Why does the program crash when using const string arguments?

My program has following code: function FooBar(const s: string): string; var sa: AnsiString; begin // .......................... sa := AnsiString(s); sa := AnsiString(StringReplace(string(sa), '*', '=', [rfReplaceAll])); sa :=…
Daniel Marschall
  • 3,739
  • 2
  • 28
  • 67
7
votes
3 answers

Defining class constant in PHP

I would like to define a class constant using a concatenation of an existing constant and a string. I can't predefine it because only scalars are allowed for predefining constants, so I currently have it as part of my constructor with a defined()…
joshs
  • 1,890
  • 1
  • 17
  • 20
7
votes
1 answer

If the constant interface anti-pattern is such a crime, why does Swing do it?

I was making a swing application, and realized I had a handful of classes that needed access to the same set of constants. I couldnt bring myself to declare one the primary holder of them and place them all in there and have the others reference it;…
captainroxors
  • 718
  • 7
  • 18
7
votes
1 answer

Is it legal to modify a dynamically-allocated `const` object through a re-used non-`const` name?

Consider the following program: #include int main() { int x = 0; const int* px = new (&x) const int(0); x = 1; std::cout << *px; // 1? } It compiles under GCC 4.8 (and produces the "expected" output), but I suspect it's…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
7
votes
2 answers

Reason for Undefined class constant NOTICE in PHP (Use of undefined constant)

I have discovered a weird problem in my code regarding class constants. While it seems that the code does work correctly, I cannot figure out the reason of PHP Notice I am getting: Use of undefined constant PAYMENT_ERROR - assumed 'PAYMENT_ERROR' in…
ek9
  • 3,392
  • 5
  • 23
  • 34
1 2 3
99
100