为什么const变量必须立即初始化? [英] Why do const variables have to be initialized right away?
问题描述
这是一个一般的程序设计问题。我正在学习C ++,我已经知道任何const变量,例如: const int i
或 int * const ptr
,必须立即初始化。
这也是引用地址必须立即初始化的根本原因,因为地址 const
。
但是我找不到为什么必须这样做的原因/
任何人都可以为我解释这个问题吗?
可以初始化它,或在稍后分配一个值。
const int size; //没有初始化(错误)
size = 100; //错误 - 你不能分配一个const变量。
现在如果一个变量既不具有任何有意义的值,也不允许它有值,因为它是一个 const 变量,那么这个变量的点是什么?这是完全无用的。
但是,这只适用于内置和POD类型:
struct A {}; // POD type
struct B {B(){}}; //非POD类型,因为它有用户定义的构造函数!
const int i; // error - built-in type
const A a; //错误 - POD类型
const B b; // ok - Non POD type
//同样
const std :: string s; // ok - std :: string是一个非POD
const std :: vector< std :: string> v; // ok - std :: vector是非POD
实际上是一个NON-POD类型 b> b 现在考虑这个结构, 可能会认为错误是因为 错误是因为 演示: =http://ideone.com/NJT8L> http://ideone.com/NJT8L 此分析还表明,即使内置类型是非POD类型的成员,内置类型也会不自动初始化 。 内置类型(和POD类型)的默认初始化语法,是这样的: 现在允许这样: 至于使用struct / class POD,请参阅this topic: This is a general programming question. I'm learning about C++ and I've learned that any const variables, ie: This is also the underlying reason that references to addresses must be initialized right away, because the addresses are But I can't find the reason why this must be done / why this rule is imposed. Can anyone explain this for me please? Because there is no way you can initialize it, or assigned with a value, later on. Now if a variable which is neither having any meaningful value, nor are you allowed to make it to have value later on because it is a const variable, then what is the point of such a variable? It is completely useless. However, this is true for only built-in and POD types: Actually a NON-POD type cannot remain uninitialized, because the default constructor will be called, and the object would get initialized. Now consider this struct, One might think the error is because of The error is because of Demo : http://ideone.com/NJT8L This analysis also demonstrates that built-in types do not get initialized automatically even if they're members of non-POD types. This is true of non-POD class types as well. And the syntax to default initialization for built-in types (and POD types) is this: Now this is allowed : Demo : http://ideone.com/84vD9 As for what makes a struct/class POD, see this topic: 这篇关于为什么const变量必须立即初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
struct C
{
const int i ;
C(){}
};
C
绝对是非POD类型,因为它有用户定义的构造函数。另外请注意,在构造函数中,它不会初始化 i 这是
int
声明为 const
。因为这个未初始化的 const
i,以下会产生错误:
const C c; //错误 -
const
在上面的变量
c
中声明。但这是短视和不是真的。即使你删除 const
,它也会给出错误:
C c ; //错误 - 相同的错误
C :: i
struct C
{
const int i;
C():i(){} //注意语法 - 它被称为成员初始化列表
};
C x; // ok
const C y; // ok
const int i
, or int *const ptr
, have to be initialized right away.const
.const int size; //no initialization (error)
size = 100; //error - you cannot assign a const variable.
struct A{}; //POD type
struct B{ B(){} }; //Non POD type because it has user-defined constructor!
const int i; //error - built-in type
const A a; //error - POD type
const B b; //ok - Non POD type
//likewise
const std::string s; //ok - std::string is a non-POD
const std::vector<std::string> v; //ok - std::vector is a non-POD
struct C
{
const int i;
C() {}
};
C
is definitely a non-POD type, because it has user-defined constructor. Also note that in the constructor, it doesn't initialize i
which is int
, declared as const
. Because of this uninitialized const
i, the following would give error:const C c; //error -
const
in the above declaration of variable c
. But that is short-sightedness and is not true. Even if you remove const
, it would give error:C c; //error - same error
C::i
which is declared const
but has not been initialized.
struct C
{
const int i;
C() : i() {} //note the syntax - it is called member-initialization list
};
C x; //ok
const C y; //ok