Dynamic objects in C ++

I come to C ++ from C # and I don't understand what a dynamic object is. So, imagine that you have class A and to create an object such as A * a = new A () is normal, but what is object a? Is it the same as an array or what? Can we write like [0] or [1]? But what happens if we overload the [] operator and want to create a dynamic object, if we have data and want to receive data in normal mode?

+4
source share
3 answers

If you write A * a = new A(), the default constructor of the class is called Aand dynamically allocates memory for one object of the class A, and the address of the allocated memory is assigned to the pointer A. Thus, the Aclass object indicates A, not an array. However, if you want an array of Aobjects dynamically, then you will need to write something like this A * a = new A[10]. This will allocate memory from 10 objects A. And you can write a[0], a[1], ... a[9]to access array objects.

, , []? [], , A - , a[1], A A, , . ( , , , ). [] , . . :

A * a = new A;
cout << a->operator[](0) << endl;

A a->operator[](0). . , ?

A * a = new A[6];
cout << a[0][0] << endl;

. a[0]

EDIT: A. jschultz410, .

+1

- , , , - .

A* a = new A;

a . () a.

+---+      +---------------+
| a | ---> | instance of A |
+---+      +---------------+

# ( Java) . ( ) .

++ :

A a;

, a a. , , a.

: a . . . .

+5

'a' - ( ) , "A". Yest . , 'a [0]' '* a'. [1] . , , , . [], :-). [] .

BTW, # 'a' () , .

, ++ . , "", , "", .

'c +' .

0
source

Source: https://habr.com/ru/post/1679817/


All Articles