什么是c ++中对象的动态初始化? [英] What is dynamic initialization of object in c++?

查看:179
本文介绍了什么是c ++中对象的动态初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是c ++中对象的动态初始化?



请用简单的示例解释...

解决方案

动态初始化是指初始化值在编译时是未知的。



示例,

  int factorial(int n)
{
if(n< 0)return -1; //表示输入错误
else if(n == 0)return 1;
else return n * factorial(n-1);
}

int const a = 10; // static initialization
// 10在编译时是已知的。其10!

int const b = factorial(8); //动态初始化
// factorial(8)在编译时不知道,
//而是在运行时计算。

也就是说,静态初始化通常包含常量表达式已知在编译时),而动态初始化涉及非常量表达式。

  static int c; // this is also静态初始化(带零)!来自C ++标准(2003)的

§3.6.2/ 1说:

$ b具有静态存储持续时间的对象
(3.7.1)在任何其他初始化之前应为零初始化
(8.5)
发生。具有常量
表达式的零初始化和
初始化统称为
静态初始化;所有其他
初始化为动态
初始化





  • 静态初始化:使用常量表达式进行零初始化或初始化
  • 任何其他初始化都是动态初始化。


还要注意,同一个变量在静态初始化后可以动态初始化。例如,请参阅以下代码:

  int d = factorial(8); 
int main()
{
}

c $ c> d 是一个全局变量,它有静态存储。这意味着,根据§3.6.2.1,它在发生任何其他初始化之前发生的静态初始化阶段被初始化为0。然后,在运行时,它使用从函数 factorial()返回的值进行动态初始化。



这意味着,全局对象可以初始化两次:一次是通过静态初始化(这是零初始化),稍后,在运行时,可以动态初始化。 / p>

What is dynamic initialization of objects in c++?

Please explain with an simple example...

解决方案

Dynamic initialization is that in which initialization value isn't known at compile-time. It's computed at runtime to initialize the variable.

Example,

int factorial(int n)
{
     if ( n < 0 )       return -1; //indicates input error
     else if ( n == 0 ) return 1;
     else               return n * factorial(n-1);
}

int const a = 10 ; //static initialization 
             //10 is known at compile time. Its 10!

int const b = factorial(8); //dynamic initialization 
                      //factorial(8) isn't known at compile time,
                      //rather it's computed at runtime.

That is, static-initialization usually involves constant-expression (which is known at compile-time), while dynamic-initialization involves non-constant expression.

static int c;//this is also static initialization (with zero)!

§3.6.2/1 from the C++ Standard (2003) says,

Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization.

So there are two kind of initializations:

  • Static initialization : Its either zero-initialization or initialization with a constant expression
  • Any other initialization is dynamic initialization.

Also note that the same variable can be dynamically-initialized after it has been statically-initialized. For example, see this code:

int d = factorial(8);
int main()
{
}

Since d is a global variable, it has static storage. That means, according to §3.6.2.1 it's initialized to 0 at the static-initialization phase which occurs before any other initialization takes place. Then later, at runtime, it's dynamically-initialized with the value returned from the function factorial().

That means, global objects can be initialized twice: once by static initialization (which is zero-initialization) and later, at runtime, they can be dynamically-initialized.

这篇关于什么是c ++中对象的动态初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆