const

C++之 const

北慕城南 提交于 2020-11-23 10:00:09
In the C, C++, and D programming languages, const is a type qualifier, a keyword applied to a data type that indicates that the data is constant (does not vary). While this can be used to declare constants, const in the C family of languages differs from similar constructs in other languages in being part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking. const was introduced by Bjarne Stroustrup in C with Classes, the predecessor to C++, in 1981, and was originally called readonly.As to motivation, Stroustrup writes

const in C++

别来无恙 提交于 2020-11-23 09:49:03
一 关于一般常量 声明或定义的格式如下: const <类型说明符> <变量名> = <常量或常量表达式>; [1] <类型说明符> const <变量名> = <常量或常量表达式>; [2] [1]和[2]的定义是完全等价的。 例如: 整形int(或其他内置类型:float,double,char) const int bufSize = 512; 或者 int const bufSize = 512; 因为常量在定义后就不能被修改,所以定义时必须初始化。 bufSize = 128; // error:attempt to write to const object const string cntStr = "hello!"; // ok:initialized const i, j = 0; // error: i is uninitialized const 非const变量默认为extern。 const 对象默认为文件的局部变量。要使const变量能够在其他的文件中访问,必须显式地指定它为extern。 例如: const int bufSize = 512; // 作用域只限于定义此变量的文件 extern const int bufSize = 512; // extern用于扩大作用域,作用域为整个源程序(只有extern 位于函数外部时,才可以含有初始化式)

Is const-casting away const-ness of references to actual const objects permitted if they are never modified through them?

喜你入骨 提交于 2020-07-01 06:56:56
问题 I have an abstract class that declares const and non-const member functions. For the sake of discussion let's say it looks like this: class record_interface { public: virtual ~record_interface() = default; virtual void set_foo(BoundedFloat) = 0; virtual BoundedFloat get_foo() const = 0; }; This is used as a high-level representation of a record that has different representations when saved to disc and transferred via the wire. So most implementations just need to convert their members to the

Initialising a static const variable from a function in c

◇◆丶佛笑我妖孽 提交于 2020-06-27 12:03:33
问题 I have recently run into some trouble while trying to perform the following logic: static const int size = getSize(); int getSize() { return 50; } The error I have received is initialiser element is not constant Having read online I understand that this issue is because the compiler evaluates the static const expression at compilation and therefore cannot know what the value is supposed to be. My question is how do I get around this? If I have a library that contains many functions but they

Why it is allowed to initialize static variable with non const here?

纵饮孤独 提交于 2020-06-08 05:57:29
问题 I was reading this. The first answer by @Andrei T says that A "large" object is never a constant expression in C, even if the object is declared as const. Const-qualified objects (of any type) are not constants in C language terminology. They cannot be used in initializers of objects with static storage duration, regardless of their type. For example, this is NOT a constant const int N = 5; /* `N` is not a constant in C */ The above N would be a constant in C++, but it is not a constant in C.

Why it is allowed to initialize static variable with non const here?

倾然丶 夕夏残阳落幕 提交于 2020-06-08 05:57:11
问题 I was reading this. The first answer by @Andrei T says that A "large" object is never a constant expression in C, even if the object is declared as const. Const-qualified objects (of any type) are not constants in C language terminology. They cannot be used in initializers of objects with static storage duration, regardless of their type. For example, this is NOT a constant const int N = 5; /* `N` is not a constant in C */ The above N would be a constant in C++, but it is not a constant in C.

How do I best use the const keyword in C?

梦想与她 提交于 2020-05-28 13:04:09
问题 I am trying to get a sense of how I should use const in C code. First I didn't really bother using it, but then I saw a quite a few examples of const being used throughout. Should I make an effort and go back and religiously make suitable variables const ? Or will I just be waisting my time? I suppose it makes it easier to read which variables that are expected to change, especially in function calls, both for humans and the compiler. Am I missing any other important points? 回答1: const is

How do I best use the const keyword in C?

孤街浪徒 提交于 2020-05-28 13:04:08
问题 I am trying to get a sense of how I should use const in C code. First I didn't really bother using it, but then I saw a quite a few examples of const being used throughout. Should I make an effort and go back and religiously make suitable variables const ? Or will I just be waisting my time? I suppose it makes it easier to read which variables that are expected to change, especially in function calls, both for humans and the compiler. Am I missing any other important points? 回答1: const is

Why is `constexpr` part of the C++14 template prototype for `std::max()`?

久未见 提交于 2020-05-28 07:55:49
问题 Per cplusplus.com, here, the default C++11 prototype for std::max() is: template <class T> const T& max(const T& a, const T& b); In the C++14 version, however constexpr was added: template <class T> constexpr const T& max(const T& a, const T& b); Why is constexpr here and what does it add? Note on possible duplicate I think my question is not a duplicate of this one (Difference between `constexpr` and `const`), because I am asking about a very specific usage of constexpr , whereas that

undefined is not an object (evaluating 'navigation.navigate')

非 Y 不嫁゛ 提交于 2020-05-23 11:55:29
问题 I am trying to navigate from one screen to other screen inside tabbar in react native. But, I am getting following error ButtonClickCheckFunction = () => { const { navigation } = this.props; navigation.navigate('detailsScreen', { detailsScreen: jsonData }); } Any suggestions? For main screens, In tab bar we have created stack, const AppStack = createAppContainer(createDrawerNavigator({ Dashboard: { screen: ProfileStack, }, Connect: { screen: Connect, }, screen1: { screen: Screen1, } }); But,