In C ++, you have the flexibility to choose the memory in which you create the object. You can create objects in an automatic region (on the stack), in a static region, or in a dynamic region. In the latter case, you get a pointer to the object and are responsible for releasing it after completion.
In contrast, all Java is a dynamic scope parameter because all objects are reference objects. In C ++, this is equivalent to using objects only through pointers and always creating them with new . When you do this in C ++, you should also populate your pointer array with new -ed objects:
myclass *array[10]; for (int i = 0 ; i != 10 ; i++) { array[i] = new myclass(); } ... for (int i = 0 ; i != 10 ; i++) { delete array[i]; }
Allowing the creation of arrays of objects in C ++ was a choice dictated by the need to allow programmers to allocate arrays of objects in the automatic area. It came with a compromise because the objects from which you are creating arrays must have default constructors. This is not ideal, because the default constructor requirement sounds arbitrary.
Java, on the other hand, is free of automatic memory requirements, so they went for a simple solution requiring you to initialize objects individually.
dasblinkenlight
source share