I have a doubt between the behavior of two arrays declarations. And it is not related to the dynamic allocation of memory, but how to access the objects of the array. I explain.
Case a:
Suppose I define an array in the following way.
Time t1(12,13,15);//los parametros del constructor son hora, minuto y segundo.
Time t2(17,15,18);
Time *pTArr[2]; //no se hace llamada al constructor.
pTArr[0]=&t1;
pTArr[1]=&t2;
pTArr[1]->print();//imprime hora:minuto:segundo.
In this case pTArr [i], is a pointer where:
-
pTArr[i]
is the address where the object i is pointing to, and -
*pTArr[i]
, is the object it points to.
Typical operation of a pointer.
Case b:
Time *pTArr=new Time[2]; // Se inicializa y se llama al construtor Time();
pTArr[0].print(); //cuando antes era pTArr[1]->print().
From what I see, in this case it would be something like this:
-
pTArr[i]
is the object it points to. -
&pTArr[i]
is the address where thei
object is pointing to.
Why do they behave differently, are not both an array of pointers to objects Time
? In case a, it is the typical behavior of pointers. But the case b, do not behave like a pointer ( puntero=dirección
, *puntero=objeto
).
Any clarification, ask. Greetings, Julio.