对于类类型的静态数组的初始化空 [英] Regarding empty initialisation of static array of class types

查看:173
本文介绍了对于类类型的静态数组的初始化空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我跑静态code分析仪QACPP,我得到了一个警告。按照QACPP文档,与初始化{0} 仅适用于内建类型。要初始化类型的对象的数组 A {} 必须使用。如下图所示:

  INT I [5] = {0}; //只适用于内置类型。
A中的[5] = {}; // OK,可与内置和类类型都

这是一个标准的C ++规则?根据这一点,指针一类类型的数组也必须用初始化{} ,对吧?

这是否语句:

  A * AP [5] = {}

初​​始化5指针与 NULL

QACPP抛出我一个警告,当我用 A * AP [5] = {NULL} ,虽然code编译和完美的作品甚至其他。


其他

我觉得警告是更多的是因为数组是静态的。

这里是我的文档中的解释是:


  

有许多问题与使用
  与静态存储的对象
  持续时间,特别是那些宣布
  功能之外。如果许多
  功能可以访问的静态对象
  这种情况下,可能变得难以
  保持。此外,在的情况下
  它成为多线程应用程序
  既要保护每个互斥
  可访问静态对象
  兼任。因此,它是一个很好的
  想法限制静态的范围
  对象尽可能,以便
  你知道在哪里这样的对象是
  访问。


  
  

与静态命名空间或类对象
  存储持续时间将被初始化
  在节目开始时,前
  调用主(),以及顺序
  初始化是不确定的。
  依赖的顺序
  初始化可导致对象
  他们以前使用
  初始化。如果抛出一个异常
  作为初始化一个的结果
  非本地对象的程序将
  立即终止。


  
  

块静态范围的对象
  存储持续时间将被初始化
  当函数首先被调用。
  因此,最好是使用
  Singleton模式,而不是命名空间
  对象和静态数据成员。这个
  嗣继承包裹在一个对象
  用作局部静态对象,并
  具有功能返回引用
  此对象。



解决方案

是的,这是一个标准的规则。数组是一个集合体。初始化的规则集料的标准被明确提及。


  

这是否声明:A * AP [5] = {}
  用NULL intialise 5指针?



  

QACPP抛出我,当我用A * AP警告[5] = {NULL}


什么警告?也许该警告是,你只初始化的第一个元素,和其他人将保持空。当然,也可能是你所需要的。但随后,编译器只是警告你:)

我觉得这个问题和答案将是非常有趣的。 <一href=\"http://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special\">What是集料和POD的,以及如何/为什么他们特别?

When I ran static code analyzer QACPP, I got a warning. According to the QACPP documentation, initializing with {0} only works for built in types. To initialize an array of objects of type A, {} must be used. As below:

int i[5] = {0};  // Only works with built-in types.
A   a[5] = {};   // OK, works with both built-in and class types

Is this a standard C++ rule? According to this, an array of pointers to a class type must also be initialized with {}, right?

Does this statement:

A* ap[5] = {}

initialise the 5 pointers with NULL?

QACPP throws me a warning when I used A* ap[5] = {NULL}, though the code compiles and works perfectly even otherwise.


Additional

I think the warning was more because the array is static.

And here is the explanation I found in the documentation:

There are many problems with the use of objects with static storage duration, particularly those declared outside of functions. If many functions can access a static object this situation may become difficult to maintain. Also, in the case of multi-threaded applications it becomes necessary to protect with mutex each static object that can be accessed concurrently. It is therefore a good idea to limit the scope of a static object as much as possible, so that you know where such object is accessed.

Namespace or class objects with static storage duration will be initialised at the start of the program, before calling main(), and the order of initialisation is unspecified. Reliance on the order of initialisation may lead to objects being used before they are initialised. If an exception is thrown as a result of initialising a non-local object the program will immediately terminate.

Block scope objects with static storage duration will be initialised when the function is first called. Therefore, it is best to use the singleton pattern instead of namespace objects and static data members. This entails wrapping the object in a function as a local static object, and having the function return a reference to this object.

解决方案

Yes, it is a standard rule. An array is an aggregate. The rules of initialization are explicitly mentioned in the standard for aggregates.

Does this statement: A* ap[5] = {} intialise the 5 pointers with NULL?

Yes

QACPP throws me a warning when I used A* ap[5] = {NULL}

What warning? Maybe the warning is that you initialized only the first element, and the others will stay NULL. Of course, it may be what you need. But then, the compiler is just warning you :)

I think this question and answer will be very interesting. What are Aggregates and POD's and how/why are they special?

这篇关于对于类类型的静态数组的初始化空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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