当前位置:网站首页>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;
}
边栏推荐
- [cmake command notes]find_ path
- Philosopher's walk gym divide and conquer + fractal
- Population standard deviation and sample standard deviation
- Uint8 serializing and deserializing pits using stringstream
- fatal: refusing to merge unrelated histories
- There are animation characters interacting with each other when the mouse slides in the web page
- Random decoding NLP
- Quick sort method
- 总体标准差和样本标准差
- Debian10 installing zabbix5.4
猜你喜欢

Unique in Pimpl_ PTR compilation errors and Solutions

Autumn move script C

An interesting example of relaxed memory models

Use elk to save syslog, NetFlow logs and audit network interface traffic
![[learning notes] roll back Mo team](/img/19/d374dd172b9609a3f57de50791b19e.png)
[learning notes] roll back Mo team

Constexpr keyword

Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe

Webdriver and selenium Usage Summary

On AI and its future trend | community essay solicitation

Error reported when compiling basalt
随机推荐
Vector 2 (friend and copy construction)
Pat class A - 1012 the best rank (PIT)
JMeter associated login 302 type interface
There are animation characters interacting with each other when the mouse slides in the web page
Foundation Consolidation - Flex width is content width
leetcode 91. Decode ways (medium)
C#. Net universal database access encapsulation classes (access, sqlserver, Oracle)
Vector 1 (classes and objects)
Unique in Pimpl_ PTR compilation errors and Solutions
Add / get and delete cookies
Pat class A - 1013 battle over cities
[luogu] p1083 [noip2012 improvement group] classroom borrowing (difference)
C# SerializableDictionary序列化/反序列化
Download and compile ROS source code
LINQ 查詢
Debian10 create users, user groups, switch users
Centos7 installing postgresql12
Modulenotfounderror: no module named 'rospy', PIP could not find the installation package
魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择
Lexical Sign Sequence