I know this may sound trivial, but I'm new to C++ OOP. Thanks a lot for help!
For example, for the following code:
using namespace std;
class Object {
public:
int arr[10];
Object(int b) {memset(arr, 0, sizeof(arr));}
};
void test(Object &first, Object other)
{
cout << "Test" << endl;
cout << (first.arr) << endl;
cout << (other.arr) << endl;
return;
}
int main()
{
Object x(2);
Object y(3);
test(x, y);
return 0;
}
Note that in the function declaration void test(Object &first, Object other)
, the first parameter takes the original alias, while the second takes a copy.
I'm aware that Object other
creates a local copy of the object passed into this function. But question is: is the member arr
copied as well? In other words, when calling test(x, y)
does y.arr
and other.arr
point to the same or different array?
If a copy is performed, does it perform shallow or deep copy?
What then is the case when instead of declaring int arr[10]
, a int *arr
is used and the arr
is dynamically allocated in the constructor, using the new
keyword?
Thanks a lot!
Edit: My findings
Is it that in the case of declaring int arr[10]
, a new array is copied, and the elements inside the new array point to the objects in the original array?
When instead new
keyword is used to dynamically allocate, the copied array is still a pointer to the array of the copied object?