do arrays hold pointers or the actual objects

D

don

My question is, do C++ array of objects hold the objects or just the
pointers to the objects..... I know Java arrays only hold pointers to
objects, but I seem to remember that C++ arrays hold the actual object.....
please set me straight......
 
J

Jonathan Turkanis

don said:
My question is, do C++ array of objects hold the objects or just the
pointers to the objects..... I know Java arrays only hold pointers to
objects, but I seem to remember that C++ arrays hold the actual object.....
please set me straight......

C++ arrays hold objects; the notion of object, however, is broad
enough to include pointers. If you have a class T, you can declare an
array of T's like

T a[N]

or an array of pointers to T like so

T* a[N]

Jonathan
 
M

Mike Wahler

don said:
My question is, do C++ array of objects hold the objects
Yes.

or just the
pointers to the objects.....

No. However, objects stored in an array can be of pointer type.
I know Java arrays only hold pointers to
objects,

C++ is not Java. Despite their 'similarities', imo it's a
mistake to try to directly 'translate' programming constructs
and concepts from one language to the other. That way lies
madness.... :)
but I seem to remember that C++ arrays hold the actual object.....
please set me straight......

Please read a C++ book. :)
http://www.accu.org/bookreviews/public/reviews/0sb/beginner_s_c__.htm

int arr1[10]; /* array of ten type 'int' objects */

int *arr2[10]; /* array of ten type 'pointer to int' objects */


Finally, note that in C++, imo one should generally prefer a container
(e.g. std::vector) over an array.

-Mike
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top