Initializing Curly Bracket in C ++ and Java

In the code below, I don't seem to understand the limitations of initializing curly braces. What are they actually doing? It seems that in case A, it simply sets the value [0] to the value directly. In case b, it uses an implicit conversion. Does he decide which one to do based on what is available, or is there some other method that he uses?

#include <iostream> using namespace std; struct A { }; struct B { B(int a) { cout << a; } }; int main() { A* a[] = {new A()}; B b[] = {1}; } 

Will the same type of curly brace initialization work the same way 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 */ }. } } 

Sorry if my question seems silly, I just want to know more about curly braces in C ++ and Java. Thank you in advance: -)

+4
source share
4 answers

Since the Java part has already answered, I will add the C ++ part a bit. The specific version of brace initialization that you call is called aggregate initialization and (not surprisingly) is used to initialize aggregates. Each element in the aggregate will be initialized with the corresponding element inside curly brackets, and you can use what you want to use, which can be implicitly converted to the type of the element in the aggregate.

There are a couple of specific parts of the function that you can consider for the specific case of arrays. The number of elements inside curly brackets cannot be greater than the size of the array, but it can be less, in which case the remaining elements will be initialized by default.

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

If the size of the array is not specified in the 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] 

Note that unlike Java, size is an integral part of an array type, so although you can enter int a[] = { 1 }; It can never be a shared array of an undefined number int .

In C ++ 11, the curly bracket syntax has been extended to provide uniform initialization, but this is probably beyond the scope of the question, I just mentioned this if you want to read more on this subject.

+5
source

This is the same as in C ++

 someClass[] sC = { new someClass(), new someClass(), new someClass() }; int[] i = { 1, 2, 3 }; String[] s = { "1", "2", "3" }; 
+2
source

I donโ€™t quite remember how this is done in C ++, but in java you can do:

 String[] array = new String[]{ "a", "b", "c" }; 

So, you do not pass arguments to the constructors, you pass the objects themselves.

+1
source

In Java, an array is a container object.

You can store values โ€‹โ€‹for primitive types and references to someClass or sub-classes objects in the someClass[] sC .

 class SomeClass {} class Foo extends SomeClass{} Foo f=new Foo(); SomeClass []sC={f,new Foo(),new SomeClass()}; 
+1
source

All Articles