In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people don't understand the knowledge points of this "Java copy constructor example analysis" article, so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this "Java copy constructor example analysis" article together.
There is one and 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;}; 分析:以上错误均为C++中最基础知识,也是最易让人迷惑的一部分,有关复制构造函数要注意的一点是深复制和浅复制的问题。针对以上函数的每一问题描述如下: 1、构造函数内部没有分配空间便开始赋值; 2、复制构造函数为浅复制,造成两个对象会共用一块内存 3、析构函数内部应删除数组内存,并将指针赋值为空 4、中括号操作符函数下标越界检验不完全,当index为负值时,程序崩溃 5、赋值操作符函数无返回值,返回值的作用是为了连续赋值a = b = c; 程序改正如下: 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, trying to call the constructor to implement deep copy is not feasible, because this will generate an anonymous object in the constructor, after the copy constructor is called, the object is destroyed (by printing characters in the destructor can be verified), so deep copy is not implemented as expected, anonymous objects are implemented deep copy. Therefore, external access to the data members of the object calling the copy constructor will result in an error.
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 content of this article about "Java copy constructor instance analysis". I believe everyone has a certain understanding. I hope that the content shared by Xiaobian will be helpful to everyone. If you want to know more related knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.