C ++和Java中的花括号初始化 [英] Curly Bracket Initialization in C++ and Java

查看:65
本文介绍了C ++和Java中的花括号初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我似乎不了解花括号初始化的局限性。他们实际上是做什么的?似乎在A的情况下,它只是直接将a [0]设置为等于该值。在b的情况下,它使用隐式转换。

In the code below, I don't seem to understand the limitations of the curly bracket initialisation. What do they actually do? It seems in the case of A it just sets a[0] equal to the value directly. In the case of b it using implicit conversion. Does it decide which one to do based on what is available, or is there some other method it uses?

#include <iostream>

using namespace std;

struct A
{

};

struct B
{
    B(int a) { cout << a; }
};

int main()
{
    A* a[] = {new A()};
    B b[] = {1};    
}

这种大括号初始化在Java中是否也会类似地工作?

Also would this type of curly bracket initialisation work similarly in Java?

public class A
{
     public static void main(String[] args)
     {
          someClass[] sC = { /* what can go here? an argument to the constructor,
                               or just a value to set the variable equal to */ }.
     }
}

对不起,如果我的问题看起来很傻,只是真的想了解有关c ++和Java中花括号的更多信息。在此先感谢您:-)

Sorry if my question seems silly, just really want to know more about curly brackets in c++ and Java. Thanks in advance :-)

推荐答案

由于Java部分已经得到解答,因此我将对C ++部分进行补充。您所指的卷曲支撑初始化的特定版本称为聚合初始化,并且(毫不奇怪)用于初始化聚合。集合中的每个元素都将使用大括号内的相应元素进行初始化,您可以使用隐式可转换为集合中元素类型的任何要使用的内容。

Since the Java part has already been answered, I will add a bit about the C++ part. The specific version of curly brace initialization that you refer to is called aggregate initialization and (unsurprisingly) is used to initialize aggregates. Each element in the aggregate will be initialized with the corresponding element inside the braces and you can use whatever you want to use that can be implicitly convertible to the type of the element in the aggregate.

对于数组的特定情况,您可能需要考虑功能的几个特定部分。大括号内的元素数不能大于数组的大小,但可以较小,在这种情况下,其余元素将被默认初始化。

There are a couple of specific parts of the feature that you might want to consider for the specific case of arrays. The number of elements inside the curly braces cannot be greater than the size of the array, but it can be smaller in which case the rest of the elements will be default initialized.

int a[5] = { 1, 2 }; // [ 1, 2, 0, 0, 0 ]

如果数组的大小不是在用户代码中提供,编译器会将其设置为 aggregate-initialization 列表中的元素数:

If the size of the array is not provided in user code, the compiler will set it to the number of elements in the aggregate-initialization list:

int a[] = { 1, 2, 3 }; // int a[3]

请注意,与Java不同,大小是Java中不可或缺的一部分数组的类型,因此尽管您可以键入 int a [] = {1}; ,但是它永远不能是的通用 数组 int 的em>不确定的数。

Note that unlike in Java, the size is an integral part of the type of the array, so that while you can type int a[] = { 1 };, it can never be a generic array of undetermined number of int.

在C ++ 11中,花括号语法已得到扩展,以提供统一初始化,但这可能超出了问题的范围,如果您想进一步了解该主题,我只是在提及它。

In C++11 the curly brace syntax has been extended to provide uniform initialization, but that is probably outside of the scope of the question, I just mention it in case you want to read more on the subject.

这篇关于C ++和Java中的花括号初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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