do arrays hold pointers or the actual objects

Discussion in 'C++' started by don, Feb 8, 2004.

  1. don

    don Guest

    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......
     
    don, Feb 8, 2004
    #1
    1. Advertising

  2. "don" <> wrote in message
    news:c047tv$ilo$...
    > 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
     
    Jonathan Turkanis, Feb 8, 2004
    #2
    1. Advertising

  3. don

    Mike Wahler Guest

    "don" <> wrote in message
    news:c047tv$ilo$...
    > 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
     
    Mike Wahler, Feb 8, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    678
    -berlin.de
    Mar 28, 2005
  2. Replies:
    10
    Views:
    757
    Daniel T.
    Feb 3, 2006
  3. Simon L
    Replies:
    3
    Views:
    426
    Jeff Flinn
    Mar 31, 2006
  4. mrstephengross
    Replies:
    5
    Views:
    394
    mrstephengross
    May 23, 2008
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    732
Loading...

Share This Page