- Creating an array from an existing array - 5 Updates
Doug Mika <dougmmika@gmail.com>: Jun 04 11:57AM -0700 Hi if I have an existing array of simple data types, and I use std::copy to create a copy of the array, the simple data types are simply copied into my second array? True/False? if I have an existing array of my own user defined objects, and I use std::copy to create a copy of the array, each element of the array is copied using my objects copy constructor? True/False? if I have an existing array of pointers, and I use the std::copy function to create a copy of the array, is each pointer going to be merely copied, or is the object (to which the pointer points to) going to have it's copy constructor invoked? Thanks Doug |
Paavo Helde <myfirstname@osa.pri.ee>: Jun 04 02:29PM -0500 Doug Mika <dougmmika@gmail.com> wrote in > if I have an existing array of simple data types, and I use std::copy > to create a copy of the array, the simple data types are simply copied > into my second array? True/False? You cannot create an array by std::copy. std::copy merely copies elements from one place to another, so the destination array must be already exisiting. Yes, it can grow a container like std::vector by using std::back_inserter, but this won't work with a C or C++ array (which has fixed size), and the destination container must still be created beforehand. But otherwise, you are right, std::copy indeed copies data (doh). > if I have an existing array of my own user defined objects, and I use > std::copy to create a copy of the array, each element of the array is > copied using my objects copy constructor? True/False? I guess it's more likely that the assignment operator gets used, unless you are using std::back_inserter or something like that. > function to create a copy of the array, is each pointer going to be > merely copied, or is the object (to which the pointer points to) going > to have it's copy constructor invoked? Raw pointers belong to "simple data types", so as per your first point they just get copied. Nothing more happens. If you want deep copying this has to be arranged specially. hth Paavo |
Doug Mika <dougmmika@gmail.com>: Jun 04 01:30PM -0700 On Thursday, June 4, 2015 at 2:29:25 PM UTC-5, Paavo Helde wrote: > has to be arranged specially. > hth > Paavo So let's say I have an array of pointers to my user defined objects MyObjects: int main(){ MyObject* myArray[3]; myArray[0]=new MyObject(); myArray[1]=new MyObject(); myArray[2]=new MyObject(); MyObject* mySecondArray[3]; mySecondArray[0]=myArray[0]; mySecondArray[1]=myArray[1]; mySecondArray[2]=myArray[2]; return 0; } will mySecondArray contain copies of the pointers in myArray (and hence point to the same objects as elements of myArray) (I hate asking this as all my logic seems to tell me yes, but for some reason I have doubts about this), and of course it makes no difference whether MyObject implements a copy assignment operator as we are dealing with pointers to MyObject and not MyObject instances, correct? |
Victor Bazarov <v.bazarov@comcast.invalid>: Jun 04 04:37PM -0400 On 6/4/2015 4:30 PM, Doug Mika wrote: > whether MyObject implements a copy assignment operator as we are > dealing with pointers to MyObject and not MyObject instances, > correct? Correct. You're assigning the values of elements of one array to elements of the other array. Once this is done, the *values* of the 0th elements are the same (and of the 1st ones, and the 2nd ones). What is the type of those elements? They are pointers. If their values are the same, it means they point to the same object. No matter how many pointers you create, if their values are all the same, they all point to the same object. The nature of the object is irrelevant. Of course, if the value is 'null', they all point to nothing. Still it's the same nothing. :-) V -- I do not respond to top-posted replies, please don't ask |
Paavo Helde <myfirstname@osa.pri.ee>: Jun 04 04:05PM -0500 Doug Mika <dougmmika@gmail.com> wrote in > doubts about this), and of course it makes no difference whether > MyObject implements a copy assignment operator as we are dealing with > pointers to MyObject and not MyObject instances, correct? Right. Maybe it will help you to notice that these assignments are essentially C code (C has pointers, arrays of pointers and 'mySecondArray [0]=myArray[0];' is valid C code assigning one pointer value to another). Of course, C does not do anything behind the scenes or automatically, so this assignment is just a copy of the pointer value and nothing else. And as C++ strives to be mostly compatible with C, it cannot do anything other with this code either (regardless of if it would make any sense or not). hth Paavo |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment