当前位置:网站首页>9. class and object practice and initialization list
9. class and object practice and initialization list
2022-06-23 01:41:00 【For financial freedom!】
class String
{
public:
String(const char* str = nullptr)
{
if(str!=nullptr)
{
m_data = new char[strlen(str) + 1];
strcpy(this->m_data,str);
}
else
{
m_data = new char[1];
*m_data = '\0';
}
}
String(const String& other)
{
m_data = new char[strlen(other.m_data)+1];
strcpy(m_data,other.m_data);
}
~String()
{
delete[] m_data;
m_data = nullptr;
}
String& operator=(const String& other)
{
if(this == &other)
{
return *this;
}
delete[] m_data;
m_data = new char[strlen(other.m_data)+1];
strcpy(m_data,other.m_data);
return *this;
}
private:
char* m_data;
}:
str3=str1=str2
The reason why continuous assignment is feasible is :String& operator=(const String& other), The return type is String&
Circular queue :
class Queue
{
public:
Queue(int size = 20)
{
_pQue = new int[size];
_front = _rear = 0;
_size = size;
}
Queue(const Queue& src)
{
_size = src._size;
_front = src._front;
_rear = src._rear;
_pQue = new int[_size];
for(int i=_front;i!=_rear;i=(i+1)%_size)
{
_pQue[i] = src._pQue[i];
}
return *this;
}
Queue& operator=(const Queue& src)
{
if(this == &src)
{
return *this;
}
delete[] _pQue;
_size = src._size;
_front = src._front;
_rear = src._rear;
_pQue = new int[_size];
for(int i=_front;i!=_rear;i=(i+1)%_size)
{
_pQue[i] = src._pQue[i];
}
return *this;
}
~Queue()
{
delete[] _pQue;
_pQue = nullptr;
}
void push(int val)
{
if(full())
{
resize();
}
_pQue[_rear]=val;
_rear = (_rear+1)%_size;
}
void pop()
{
if(empty())
{
return;
}
_front = (_front + 1)%_size;
}
int top()
{
return _pQue[_front];
}
bool full()
{
return (_rear+1)%_size == _front;
}
bool empty()
{
return _front == _rear;
}
private:
int* _pQue;
int _front;// Team leader position
int _rear;// At the end of the line
int _size;
void resize()
{
int* ptmp = new int[2 * _size];
int index = 0;
for(int i=_front;i!=_rear;i=(i+1)%_size)
{
ptmp[index++] = _pQue[i];
}
delete[] _pQue;
_pQue = ptmp;
_front = 0;
_rear = index;
_size *= 2;
}
};
Initialization list , Determines how member variables are initialized ! If the member variable is a class object , The class constructor of this class object is not the default , There is no default , such as :
class CDate
{
CData(int x,int y,int z);
};
class CGoods
{
public:
CGoods(int price):_date(1,2,3),_price(price)
{
}
private:
CDate _date;
int _price;
}
// This is legal , Because the initialization list is executed first , and CGoods():_date(1,2,3) and
//CDate _date(1,2,3) It's the same in assembly
// If the above does not initialize the list ,CDate _date; This will report an error , Because the default constructor is not provided or the constructor default value is not provided

The execution order of the initialization list depends on the order of the member variables , Instead of initializing the order of the list !
边栏推荐
- The road of architects starts from "storage selection"
- Big guys, 2.2.1 the CDC monitoring SQLSEVER can only get the full amount of data. Why can't we get the incremental data in the later period
- Day260: the number III that appears only once
- Cmake passing related macros to source code
- SYSTEMd summary
- Debian10 create users, user groups, switch users
- Wechat mobile terminal development - account login authorization
- [ZOJ] P3228 Searching the String
- Prevent others from using the browser to debug
- Constexpr keyword
猜你喜欢

The road of architects starts from "storage selection"

SQL programming task03 job - more complex query

3D printing microstructure

SQL programming task05 job -sql advanced processing

07 project cost management

Day367: valid complete square
![Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;](/img/75/d2ad171d49611a6578faf2d390af29.jpg)
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;

Debian10 configuring rsyslog+loganalyzer log server

office2016+visio2016

Template specialization template <>
随机推荐
It's still like this
You can also do NLP (classification)
[template] KMP
Charles garbled code problem solving
Epoll introduction and principle explanation
Use elk to save syslog, NetFlow logs and audit network interface traffic
SQL programming task03 job - more complex query
Day260: the number III that appears only once
JS image resolution compression
HDU - 7072 double ended queue + opposite top
How to type Redux actions and Redux reducers in TypeScript?
Pat a - 1010 radical (thinking + two points)
2D prefix and
Similar to attention NLP
You can also do NLP (classification)
Char[], char *, conversion between strings
OOP multiple storage (class template)
Cmake passing related macros to source code
ERROR { err: YAMLException: end of the stream or a document separator is expected at line 6, colum
Js--- SVG to png