copy array of classes or explicity call constructor

This is a discussion on copy array of classes or explicity call constructor within the C++ Programming forums, part of the General Programming Boards category; I have a dynamic array of classes (Buffer to be exact). When this array is full, I want to extend ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    54

    copy array of classes or explicity call constructor

    I have a dynamic array of classes (Buffer to be exact). When this array is full, I want to extend it. Unfortunately, since C++ has no equivalent to realloc() that works on classes, I have to allocate a new[] array and then copy everything and then delete[] the old array. Being not well trained in the ways of C++, I'm clueless on how to copy an array of classes. Does it work like copying an array of structs in C? The other way I could think of doing this is somehow explicitly calling the copy constructor of each new element.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    A for loop, and per array element assignment.

    > Does it work like copying an array of structs in C?
    Yes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    So I can just use memcpy?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    No, use the proper class assignment operator, if you have one.

    for ( i = 0 ; i < howmany ; i++ ) newarr[ i ] = oldarr [ i ];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You might use a STL container for the job. Then you'll just have to worry about copy constructors / assignment operators if your class requires them.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Yes, the standard C++ vector class does all the "realloc" stuff for you. Beyond a learning exercise or some very specific coding requirements, you should really be using that over a dynamic array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  4. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 10:15 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21