Local copy of array Element

Kefir

To make my code more readable, i have a local variable inside a function, that takes a specified element from an array, like this:

Element* elements = new Element[10];

void doSomething(int index) {
    Element element = elements[index];
    // do things with that element
}

What happens here: is element an independent copy of elements[index] that gets destroyed at the end of the function? From what I've tested, it seems like it, as changes in element don't affect element[index], however, I'd like to know what's happening behind the scenes. Does the assignment of element call an implicit copy constructor?

Mike Seymour

Yes, that's exactly what happens (although this is an initialisation, not an assignment).

You've declared Element to be an object, so it's a separate object from any other Element. It's initialised by copying its initialiser elements[index]. If you haven't delcared a copy-constructor, then it uses the implicit one, copying each member.

If you wanted to modify the element in the array, then you'd want a reference:

Element & element = elements[index];
        ^

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy to new array and remove element?

From Dev

Array copy exclude each x element

From Dev

copy array without specified element java

From Dev

Java - How to copy each element of array to ArrayList?

From Dev

Array copy loop makes every element the same

From Dev

Copy one element of string array into another java

From Dev

Copy array element modify and add again

From Dev

Copy array (passed as a function argument) elements into another local array in JavaScript

From Dev

How to delete an array element stored in local storage

From Dev

Make array element copy it's previous element's value

From Dev

is there any possibility to alter an array element by modifying its copy?

From Dev

Create Array reference with element index instead of value copy

From Dev

Copy element from one array and move the value to another

From Dev

xslt 2 copy parent element based on input array with loop

From Dev

Copy local file in Saltstack

From Dev

Magento copy controller to local

From Dev

DLL property Copy to local

From Dev

Copy hangs in local network

From Dev

Search for a specific element in one array and copy the entire corresponding row in other array

From Dev

How to remove an element in a character array or create a copy of a new array without specific elements?

From Dev

Xslt copy element into renamed element

From Dev

using remote copy (scp) to perform local copy

From Dev

copy array of pointers and print the copy

From Dev

COPY local commits to another branch

From Dev

How to copy docker volume to local?

From Dev

Git without complete local copy

From Dev

Is "Copy Local" transitive for project references?

From Dev

Copy database to the local machine - ORACLE

From Dev

Copy VB Datatable To Local Database

Related Related

  1. 1

    Copy to new array and remove element?

  2. 2

    Array copy exclude each x element

  3. 3

    copy array without specified element java

  4. 4

    Java - How to copy each element of array to ArrayList?

  5. 5

    Array copy loop makes every element the same

  6. 6

    Copy one element of string array into another java

  7. 7

    Copy array element modify and add again

  8. 8

    Copy array (passed as a function argument) elements into another local array in JavaScript

  9. 9

    How to delete an array element stored in local storage

  10. 10

    Make array element copy it's previous element's value

  11. 11

    is there any possibility to alter an array element by modifying its copy?

  12. 12

    Create Array reference with element index instead of value copy

  13. 13

    Copy element from one array and move the value to another

  14. 14

    xslt 2 copy parent element based on input array with loop

  15. 15

    Copy local file in Saltstack

  16. 16

    Magento copy controller to local

  17. 17

    DLL property Copy to local

  18. 18

    Copy hangs in local network

  19. 19

    Search for a specific element in one array and copy the entire corresponding row in other array

  20. 20

    How to remove an element in a character array or create a copy of a new array without specific elements?

  21. 21

    Xslt copy element into renamed element

  22. 22

    using remote copy (scp) to perform local copy

  23. 23

    copy array of pointers and print the copy

  24. 24

    COPY local commits to another branch

  25. 25

    How to copy docker volume to local?

  26. 26

    Git without complete local copy

  27. 27

    Is "Copy Local" transitive for project references?

  28. 28

    Copy database to the local machine - ORACLE

  29. 29

    Copy VB Datatable To Local Database

HotTag

Archive