Home  >  Article  >  Java  >  Detailed explanation of copy constructor examples in Java

Detailed explanation of copy constructor examples in Java

WBOY
WBOYforward
2023-04-23 22:58:051557browse

Title: There is only one error in each function in the definition of the following integer array class. Find and correct it

class ArrayInt {
public:
	ArrayInt(const int *pArray, int size) : m_nSize(size) {
		assert(size > 0);
		for (int i = 0; i < size; ++ i)
			m_pArray[i] = pArray[i];
	}
	ArrayInt(const ArrayInt &iCopy):m_nSize(iCopy.m_nSize),m_pArray(iCopy.m_pArray){}
	~ArrayInt() {
		delete m_pArray;
	}
	int operator[](short index) {
		assert(index < m_nSize);
		return m_pArray[index];
	}
	const ArrayInt & operator=(const ArrayInt & iCopy) {
		assert(m_nSize >= iCopy.m_nSize);
		for (int i = 0; i < m_nSize; ++ i)
			m_pArray[i] = iCopy.m_pArray[i];
	}
private:
	short m_nSize;
	int * m_pArray;
};

Analysis: The above errors They are the most basic knowledge in C and the most confusing part. One thing to note about the copy constructor is the issue of deep copy and shallow copy. Each problem of the above functions is described as follows:

1. The assignment starts without allocating space inside the constructor;

2. The copy constructor is a shallow copy, causing the two objects to share a piece. Memory

3. The array memory should be deleted inside the destructor, and the pointer should be assigned empty

4. The bracket operator function subscript out-of-bounds check is incomplete, when the index is a negative value , the program crashes

5. The assignment operator function has no return value. The function of the return value is to continuously assign a = b = c;

The program correction is as follows:

class ArrayInt {
public:
	ArrayInt(const int *pArray, int size) : m_nSize(size) {
		assert(size > 0);
		m_pArray = new int[size];
		for (int i = 0; i < size; ++ i)
			m_pArray[i] = pArray[i];
	}
	ArrayInt(const ArrayInt & iCopy) {
		//ArrayInt(iCopy.m_pArray, iCopy.m_nSize);
		m_nSize = iCopy.m_nSize;
		assert(m_nSize > 0);
		m_pArray = new int[m_nSize];
		for (int i = 0; i < m_nSize; ++ i)
			m_pArray[i] = iCopy.m_pArray[i];
	}
	~ArrayInt() {
		if (m_pArray) {
			delete[] m_pArray;
			m_pArray = NULL;
		}
		//printf("distructor is called\n");
	}
	int operator[](short index) {
		assert(index < m_nSize && index >= 0);
		return m_pArray[index];
	}
	const ArrayInt & operator=(const ArrayInt & iCopy) {
        if (this == &iCopy) return *this;
		assert(m_nSize >= iCopy.m_nSize);
		for (int i = 0; i < iCopy.m_nSize; ++ i)
			m_pArray[i] = iCopy.m_pArray[i];
		return *this;
	}
private:
	short m_nSize;
	int * m_pArray;
};

Note: In the copy constructor, it is not feasible to try to call the constructor to implement deep copy. The reason is that an anonymous object will be generated in the constructor. After the copy constructor is called, The object is destructed (you can verify it by printing characters in the destructor), so deep copying is not implemented as expected. Anonymous objects are deep copied. Therefore, when externally accessing the data members of the object that calls the copy constructor, an error will be reported.

The test function is as follows:

void test_construct_copy() {
	int pArray[] = {1, 2, 3, 5};
	ArrayInt arr(pArray, sizeof pArray / sizeof(int));
	printf("%d \n", arr[2]);
	ArrayInt arr2(arr);
	printf("%d \n", arr2[2]);
	pArray[2] = 8;
	ArrayInt arr3(pArray, 4);
	printf("%d \n", arr3[2]);
	arr3 = arr2;
	printf("%d \n", arr3[2]);
	pArray[2] = 10;
	ArrayInt arr4(pArray, 4);
	arr3 = arr2 = arr4;
	printf("%d \n", arr3[2]);

}

The above is the detailed content of Detailed explanation of copy constructor examples in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete