C++ ES1en = assignment operator smart pointer preparation
- 2020-06-03 07:43:45
- OfStack
Assignment operator (=)
By default, the compiler overloads the (=) assignment operator for each class The default (=) assignment operator completes only a shallow copy The default assignment operator has the same meaning as the default copy constructorNote for the (=) assignment operator
The first step is to determine if the two operands are equal
The return value 1 must be
return *this
; The return type is Type
&
Type, avoid bug after continuous use of =
Such as:
class Test{
int *p;
Test(int i)
{
p=new int(i);
}
Test& operator = (const Test& obj)
{
if(this!=obj)
{
delete p;
p=new int(*obj.p);
}
return *this;
}
};
Class functions provided by the compiler by default
Includes: constructor, destructor, copy constructor, (=) assignment operator
Smart Pointers
Origin of the smart pointer
In the previous C program, when using functions such as malloc() to dynamically apply for heap space, if the memory no longer needed is not released in time, there will be a memory leak. If there is too much memory leak,
It will directly cause the equipment to stop running, especially the embedded devices, some of which may have to run for several months on power.
In C++, smart Pointers were introduced to reduce memory leaks
introduce
Pay attention to
Such as ptr -
>
value -
>
:
When the type of ptr is a normal pointer type, it is equivalent to:
(*ptr).mem
When the type of ptr is class, it is equivalent to
:(ptr.operator->())->value
Is equivalent to:
( *(ptr.operator->()) ).value
So -
>
The return type of the operator function is type*, and the return value is 1 pointer variable itself (without *)
The next example points to a smart pointer of type int
#include <iostream>
using namespace std;
class Point{
int *p;
public:
Point(int *p=NULL)
{
this->p = p;
}
int* operator -> ()
{
return p;
}
int& operator *()
{
return *p;
}
~Point()
{
cout<<"~Point()"<<endl;
delete p;
}
};
int main()
{
for(int i=0;i<5;i++)
{
Point p=new int(i);
cout <<*p<<endl;
}
return 0;
}
Run print:
0
~Point()
1
~Point()
2
~Point()
3
~Point()
~Point()
As you can see from the results, Point p will automatically call the destructor to free the memory used before it is redefined, thus avoiding the occurrence of wild Pointers.
Next, let's refine the above code so that it can be assigned.
#include <iostream>
using namespace std;
class Point{
int *p;
public:
Point(int *p=NULL)
{
this->p = p;
}
bool isNULL()
{
return (p==NULL);
}
int* operator -> ()
{
return p;
}
int& operator *()
{
return *p;
}
Point& operator = (const Point& t)
{
cout<<"operator =()"<<endl;
if(this!=&t)
{
delete p;
p = t.p;
const_cast<Point&>(t).p=NULL;
}
return *this;
}
~Point()
{
cout<<"~Point()"<<endl;
delete p;
}
};
int main()
{
Point p=new int(2);
Point p2;
p2= p; // Is equivalent to p2.operator= (p);
cout <<"p=NULL:"<<p.isNULL()<<endl;
*p2+=3; // Is equivalent to *(p2.operator *())=*(p2.operator *())+3;
//p2.operator *() return 1 a int Pointer to the , It doesn't call Point Of the class = The operator
cout <<"*p2="<<*p2 <<endl;
return 0;
}
Run print:
operator =()
p=NULL:1 // Point p members have been released
*p2=5
~Point()
~Point()
The downside, however, is that the smart pointer only points to int types, not to other types.
conclusion
Above is the site to introduce C++-(=) assignment operator, smart pointer preparation, hope to help you, if you have any questions please leave me
Say, this site will reply everybody in time. Thank you very much for your support!