The fourth week of learning: operator overloading and friends and variable-length arrays

Section One
  1. Give multiple meanings to existing operators, so that the same operator can cause different types of behaviors for different types of data or operands.
  2. The essence is function overloading
  3. Can be overloaded as ordinary functions and member functions
  4. Convert the operand of the operator to the parameter of the operator function
  5. Operators can be overloaded multiple times and called according to the type of the actual parameter.
Section Two
  1. The assignment operator is overloaded and can only be overloaded as a member function
  2. Function to assign two different types of data
  3. The assignment operator is different from the copy constructor. One is already initialized, and the other is not yet initialized.

Shallow copy and deep copy

  1. When the data of the class is char* str1, the constructor is new a piece of new memory for str. If it is a shallow copy, the data str2 of the new class also points to the same memory space, and a memory space is pointed by two pointers, which will cause various troubles. (For example, one pointer is deleted once, and the other pointer continues to be deleted, which will not work)
  2. Deep copy, you have to redefine it yourself. Delete the original space, make a new one, and copy it again. (Do n't remember to judge whether it is itself, to judge the address. Avoid making mistakes, because s=s, but if you delete s, then s is lost and cannot be assigned )
  3. Return the class object reference, try to keep the original nature.
  4. The copy constructor is also divided into shallow and deep copy.
Friends of the third quarter
  1. Overloading as a member function cannot meet the requirements, but overloading as a global function cannot call private, then overloading as friend
class T
{
    
    
	private:
		char* ptr;
	public:
		T() 
		{
    
    
			ptr = new char[5];
			strcpy(ptr,"NONE");
		}
		T(char ch[])
		{
    
    
			ptr = new char[strlen(ch)+1];
			strcpy(ptr,ch);
		}
		T& operator=(const T& t)
		{
    
    
			if(this == &t)
				return *this;
			delete [] ptr;
			ptr = new char[strlen(t.ptr)+1];
			strcpy(ptr,t.ptr) ;
		 } 
		 ~T() {
    
    delete [] ptr;}
		 void show() {
    
    cout<<ptr<<endl;}
		 T& operator+=(const T& t)  
		 {
    
    
		 	int n=strlen(ptr);
		 	char ch[n];
		 	strcpy(ch,ptr);
		 	delete [] ptr;
		 	ptr = new char[strlen(t.ptr)+n+1];
		 	strcpy(ptr,ch);
		 	strcat(ptr,t.ptr);
		 	return *this;
		 }
};

int main()
{
    
    
	T t1,t2("Jeff");
	t1= t2;
	t1.show();
	t2.show();
	t1+=t2;
	t1.show();
}

Insert picture description here

The fourth section variable-length array class implementation
#include<iostream>
using namespace std;
class Array
{
    
    
	private:
		int* a;
		int size;
	public:
		Array():size(0)
		{
    
    
			a = new int[0];
			a = nullptr;
		}
		Array(const Array& b):size(b.size)
		{
    
    
			a = new int[size];
			memcpy(a,b.a,sizeof(int)*b.size);
		}
		~ Array()
		{
    
    
			if(a)
				delete [] a;
		}
		Array& operator=(const Array& b)
		{
    
    
			if(a == b.a)
				return *this;
			if(b.a==nullptr)
			{
    
    
				if(a)
					delete [] a;
				a =nullptr;
				size =0;
				return *this;
			}
			if(size<b.size)
			{
    
    
				if(a)
					delete [] a;
				a = new int[b.size];
			}
			memcpy(a,b.a,sizeof(int)*b.size);
			size = b.size;
			return *this;
		}
		void push_back(int n)
		{
    
    
			if(a)
			{
    
    
				int* temp = new int[size+1];
				memcpy(temp,a,sizeof(int)*(size)) ;
				delete [] a;
				a = temp;
				}
			else
			{
    
    
				a= new int[1];
			}
			a[size++] = n;
		}
		
		int length() {
    
    
			return size;
		}
		
		int& operator[](int i)
		{
    
    
			return a[i];
		}
};

int main()
{
    
    
	Array a1;
	for(int i=0;i<5;i++)
		a1.push_back(i);
	Array b,c;
	b=a1;
	for(int i=0;i<a1.length();i++)
		cout<<b[i]<<" ";
	b=c;
	for(int i=0;i<b.length();i++)
		cout<<b[i]<<" ";
	cout<<endl;
	a1[3]=100;
	Array d(a1);
	for(int i=0;i<d.length();i++)
		cout<<d[i]<<" ";
	return 0;
}
  1. memcpy(目标ptr,复制ptr,sizeof(int)* size)Do array copy

Guess you like

Origin blog.csdn.net/ZmJ6666/article/details/108561712
Recommended