Questions regarding the C++ standard

A

aegis

If I have a reference type such as:
T& obj;

and assuming it is initialized to an object
of type T, then the object type of the following
expression:
&obj;

would be "Pointer to T" or "Pointer to T&"?

Also, if I dynamically allocate an array of
pointers to T, as follows:
T **p = new T*[n] for some integer n
and further populate it with pointer
values to T objects, then does:
delete [] p
handle the freeing of memory for all T objects
and then the space for the array of T* objects?
 
A

Alf P. Steinbach

* aegis:
If I have a reference type such as:
T& obj;

and assuming it is initialized to an object
of type T, then the object type of the following
expression:
&obj;

would be "Pointer to T" or "Pointer to T&"?

Except that macros don't respect scopes and such things you can think of

T& obj = otherObject;

as being translated to or equivalent to

T* const __pobj = &otherObject;
#define obj (*__pobj)

Thus when you do &obj it's the same as

&(*__pobj)

which is effectively the same as

&(*&otherObject)

And I guess you can figure it out from that.

It provides a general platform for figuring out the answer to (almost) any
question you have about references.

Also, if I dynamically allocate an array of
pointers to T, as follows:
T **p = new T*[n] for some integer n
and further populate it with pointer
values to T objects, then does:
delete [] p
handle the freeing of memory for all T objects
and then the space for the array of T* objects?

No.


Cheers & hth.,

- Alf
 
A

aegis

* aegis:
If I have a reference type such as:
T& obj;
and assuming it is initialized to an object
of type T, then the object type of the following
expression:
 &obj;
would be "Pointer to T" or "Pointer to T&"?

Except that macros don't respect scopes and such things you can think of

   T& obj = otherObject;

as being translated to or equivalent to

   T* const __pobj = &otherObject;
   #define obj (*__pobj)

Thus when you do &obj it's the same as

   &(*__pobj)

which is effectively the same as

   &(*&otherObject)

And I guess you can figure it out from that.

It provides a general platform for figuring out the answer to (almost) any
question you have about references.

Thanks.
Also, if I dynamically allocate an array of
pointers to T, as follows:
T **p = new T*[n] for some integer n
and further populate it with pointer
values to T objects, then does:
delete [] p
handle the freeing of memory for all T objects
and then the space for the array of T* objects?

No.

So it just frees the allocated space for the array
of pointer to T objects?
 
S

SG

Also, if I dynamically allocate an array of
pointers to T, as follows:
T **p = new T*[n] for some integer n
and further populate it with pointer
values to T objects, then does:
delete [] p
handle the freeing of memory for all T objects
and then the space for the array of T* objects?

So it just frees the allocated space for the array
of pointer to T objects?

Yes. The elements of this array are correctly destroyed and the memory
of this array is freed. It's just that if you destroy a pointer it
doesn't magically free the memory it points to. A raw pointer doesn't
have a destructor that does so.
 
J

Juha Nieminen

aegis said:
would be "Pointer to T" or "Pointer to T&"?

There's no such a thing in C++ as "pointer to T&". That should answer
your question.
Also, if I dynamically allocate an array of
pointers to T, as follows:
T **p = new T*[n] for some integer n
and further populate it with pointer
values to T objects, then does:
delete [] p
handle the freeing of memory for all T objects
and then the space for the array of T* objects?

Is there a delete for each new you did?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top