当前位置:网站首页>8. destruct, construct, deep copy, shallow copy, assignment operator overload
8. destruct, construct, deep copy, shallow copy, assignment operator overload
2022-06-23 01:40:00 【For financial freedom!】
OOP Implement a sequential stack
class SeqStack
{
public:
// Constructors
// With parameters , So it can be overloaded , There are multiple constructors
SeqStack(int size = 10)
{
_pstack = new int[size];
_top = -1;
_size = size;
}
// Destructor
~SeqStack()
{
delete[] _pstack;
_pstack = nullptr;
}
void push(int val)
{
if(full())
resize();
_pstack[++_top] = val;
}
void pop()
{
if(empty())
return ;
--_top;
}
int top()
{
return _pstack[_top];
}
bool empty()
{
return _top == -1;
}
bool full()
{
return _top == _size-1;
}
private:
int* _pstack;// Dynamic array , Storage order stack element
int _top;// Point to the position of the element at the top of the stack
int _size;// Total size of array expansion
void resize()
{
// According to the original 2 Double expansion
int* ptmp = new int[_size * 2];
for(int i=0;i<_size;++i)
{
ptmp[i] = _pstack[i];
}
delete[] _pstack;
_pstack = ptmp;
_size *= 2;
}
};
The construction order is opposite to the destruction order !
Destructors can call themselves :
s1.~SeqStack();
But after the call , The object doesn't exist ! Therefore, it is not recommended to call destructors by yourself !

that new Well ?
malloc Open up memory +SeqStack(60) Object construction !
Parameters of member methods , The compiler will automatically add a this The pointer !
The default copy constructor of an object is to copy data in memory ! The key is if the object occupies external resources , Then shallow copy is a problem !
// Custom copy constructor
SeqStack(const SeqStack& src)
{
_pstack = new int[src._size];
for(int i=0,i<_top;i++)
{
_pstack[i] = src._pstack[i];
}
_top = src._top;
_size = src._size;
}
// Assignment operator = heavy load
void operator=(const SeqStack& src)
{
if(this == &src)
return;
delete[] _pstack;
_pstack = new int [src._size];
for(int i=0;i<=src._top;++i)
{
_pstack[i] = src._pstack[i];
}
_top = src._top;
_size = src._size;
}
边栏推荐
- Questions not written in the monthly contest
- Download and compile ROS source code
- Lexical Sign Sequence
- Module 8 job
- Add / get and delete cookies
- Requête linq
- Day260: the number III that appears only once
- Google benchmark user manual and examples
- [luogu] p1083 [noip2012 improvement group] borrow classroom (line segment tree)
- fatal: refusing to merge unrelated histories
猜你喜欢

How are pub and sub connected in ros1?

Network module packaging
![[learning notes] roll back Mo team](/img/19/d374dd172b9609a3f57de50791b19e.png)
[learning notes] roll back Mo team

07 project cost management

Lexical Sign Sequence

Autumn move script a

Express framework installation and start service

3D printing microstructure

Autumn move script C

An interesting example of relaxed memory models
随机推荐
Random decoding NLP
Day367: valid complete square
Vscade personalization: let a cute girl knock the code with you
Quick sort method
HDU - 7072 double ended queue + opposite top
Day575: divide candy
Local deployment and problem solving of IIS in ArcGIS JS 4.23
Uint8 serializing and deserializing pits using stringstream
2D prefix and
魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择
Google benchmark user manual and examples
SQL programming task06 assignment - Autumn recruit secret script ABC
Is it safe for Hongyuan futures to open an account? Can Hongyuan futures company reduce the handling fee?
Pat class A - 1015 reversible primes
Thead safety experience
leetcode 91. Decode ways (medium)
What aspects of language and database knowledge are needed to build a web Kanban!
On AI and its future trend | community essay solicitation
Time complexity
LeetCode 206. Reverse linked list (iteration + recursion)