Issues to be aware of

Data Structure: A collection of data elements that have one or more specific relationships to each other .

A const member (const A a) cannot access a non-const function
const function can not modify its data members 


deep copy and shallow copy?

Deep copy and shallow copy can be simply understood as: if a class has resources, when the object of this class is copied, the resources are redistributed, and this process is deep copy.
Conversely, without reallocating resources, it is a shallow copy.
When a member variable in a class needs to dynamically open up memory space (ie, a pointer), it is necessary to use the deep copy method
m_pArr as a pointer variable.
Shallow copy: B inherits A, then B's pointer variable points to the space applied for by A's pointer variable. When B is released, the pointer in A is a wild pointer.
    Array( const Array &arr){ /// Copy constructor 
        m_iCount = arr.m_iCount;
        m_pArr = arr.m_pArr; /// Two pointers point to the same piece of memory 
        cout<< " Array & " << endl;
    }

Deep copy:
Array( const Array &arr){ /// Copy constructor 
      m_iCount = arr.m_iCount;
      m_pArr = new int[m_iCount];
      for(int i=0;i<m_iCount;i++) m_pArr[i]=arr.m_pArr[i];
        cout<<"Array &"<<endl;
    }

Difference between reference and pointer?
1. The reference cannot be null and must be assigned a value during initialization. 
Pointers can point to null, but are not safe.
Therefore, the use of references is efficient, because the reference does not need to verify its legality
. 2. The pointer can be reassigned to point to a different object. The reference always points to the object specified at initialization and cannot be changed.
string s1 = ("Nancy");
string s2 = ("Clandy");
string * p = &s1;
string &r = s1;
r = s2; //r still references s1 but s1 has the value "Clandy"
p = s1 ;//p points to s2, the value of s1 remains unchanged
3. A pointer is an entity that needs to allocate space, and a reference is an alias of a variable, and memory space will not be allocated
4. Multi-level pointers but no multi-level references.
5. The self-increment operation of a pointer and a reference is different. The pointer is the next address, and the reference is the variable value plus one
. 6.sizeof (reference) returns the size of the pointed variable, and the pointer returns itself.
7. Reference access to a variable is direct access, pointer is indirect access.
Use: When we want to point to an object and don't want to change its pointing , or to prevent unnecessary semantic misunderstandings when overloading operators , use references.
The rest use pointers. That is, there may be cases where it does not point to the object, and it needs to point to different objects at different times.
Example of overloaded operator: [] returns a target object that can be assigned
vector<int> vec(10);
vec[5] = 6;
*vec[5] = 6;//If the pointer is returned, write it like this, it looks like a vector pointer.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326087501&siteId=291194637