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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5945897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:13:10  来源:igfitidea点击:

What is dynamic initialization of object in c++?

c++objectdynamicinitialization

提问by Mahi

What is dynamic initialization of objects in c++?

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

Please explain with an simple example...

请用一个简单的例子来解释...

回答by Nawaz

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 usuallyinvolves 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,

C++ 标准 (2003) 的 §3.6.2/1 说,

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 expressionare collectively called static initialization; all other initialization is dynamic initialization.

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

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 dis a global variable, it has static storage. That means, according to §3.6.2.1it'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().

由于d是一个全局变量,它具有静态存储。这意味着,根据§3.6.2.1在任何其他初始化发生之前发生的静态初始化阶段初始化为 0 。然后,在运行时,它使用从函数返回的值动态初始化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.

这意味着,全局对象可以被初始化两次:一次是通过静态初始化(即零初始化),然后在运行时,它们可以被动态初始化。

回答by Arun Kumar

Dynamic initialization means the first value assigned to the variable after memory allocation is not known at compile time, it is evaluated only at run time. for example

动态初始化意味着内存分配后分配给变量的第一个值在编译时未知,仅在运行时评估。例如

#include <iostream.h>

using namespace std;

int sample()
{
    int x;
    cin >> x;
    return x;
}

const int t = sample(); //dynamic initialization

int p = sample();       //dynamic initialization

void main()

{

    cout << t;

    cout << p;

} 

As we know that a constant can get value only once i.e. at the time of initialization. this example shows that even a global variable which is static storage if dynamically initialize by return value of a function, the first value assigned to the variable is the value returned by function, which replaces the initial default value 0 of the variable which is assigned at the time of memory allocation.

正如我们所知,常量只能在初始化时获得一次值。这个例子表明,即使是静态存储的全局变量,如果通过函数的返回值动态初始化,分配给变量的第一个值是函数返回的值,它替换了分配给变量的初始默认值 0内存分配的时间。

回答by KevRhon Inc.

Initialization of a variable at the run time from the keyboard is known as Dynamic Initialization.

在运行时从键盘初始化变量称为动态初始化。

Program code:-

程序代码:-

 int a=cube(n);

In the above program code , ais a global variable to which a number nis dynamically assigned through a function cube, where cube()performs the cube of a number.

在上面的程序代码中,a是一个全局变量,n通过函数动态分配一个数字cubecube()执行数字的立方。

This is an example of Dynamic Initialization.

这是动态初始化的一个例子。

回答by Nousheen

The dynamic initialization means that the initial values may be provided during run time. Even class objects can be initialized dynamically. I.e. with the values provided at run time. :-))

动态初始化意味着可以在运行时提供初始值。甚至类对象也可以动态初始化。即使用在运行时提供的值。:-))