Cast vector to vector

If you have a const vector<int> you cannot modify the container, nor can you modify any of the elements in the container. You don’t need a const vector<const int> to achieve those semantics.

When, if ever, should we use const?

I believe the only time “const” is appropriate is when there is a spec that you’re coding against that is more durable than the program you’re writing. For instance, if you’re implementing the HTTP protocol, having a const member for “GET” is appropriate because that will never change, and clients can certainly hard-code that into … Read more

When would I use const volatile, register volatile, static volatile in C++?

register volatile int T=10; volatile qualifier means that the compiler cannot apply optimizations or reorder access to T, While register is a hint to the compiler that T will be heavily used. If address of T is taken, the hint is simply ignored by the compiler. Note that register is deprecated but still used. Practical … Read more

How to initialize const in a struct in C (with malloc)

You need to cast away the const to initialize the fields of a malloc’ed structure: struct deneme *mydeneme = malloc(sizeof(struct deneme)); *(int *)&mydeneme->a = 15; *(int *)&mydeneme->b = 20; Alternately, you can create an initialized version of the struct and memcpy it: struct deneme deneme_init = { 15, 20 }; struct deneme *mydeneme = malloc(sizeof(struct … Read more

Why does const int main = 195 result in a working program but without the const it ends in a segmentation fault?

Observe how the value 195 corresponds to the ret (return from function) instruction on 8086 compatibles. This definition of main thus behaves as if you defined it as int main() {} when executed. On some platforms, const data is loaded into an executable but not writeable memory region whereas mutable data (i.e. data not qualified … Read more

Initializing a static const array of const strings in C++

First of all, you need an array, not a pointer. static const char * const days[] = {“mon”, “tue”, “wed”, “thur”, “fri”, “sat”, “sun”}; Second of all, you can’t initialize that directly inside the class definition. Inside the class definition, leave only this: static const char * const days[]; //declaration Then, in the .cpp file, … Read more

what is the best way to define constant variables python 3 [duplicate]

Constants (in a sense) in Python 3.8+ Python 3.8 introduces the typing.Final type qualifier, which is used to indicate that a variable or attribute should not be reassigned, redefined, or overridden. PEP 591 — Adding a final qualifier to typing from typing import Final # Annotate module variables # (with or without an explicit type, … Read more

Using consts in static classes

UPDATE: This question was the subject of my blog on June 10th, 2010. Thanks for the great question! why was the decision made to not force constants to use the static modifier if they are considered static? Suppose constants are considered to be static. There are three possible choices: Make static optional: “const int x…” … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)