构造
#include<iostream>
#include<list>
using namespace std;
class A
{
private:
int a;
int *buf;
public:
A(){//构造
a = 0;
buf = new int[100];
cout << "A" << endl;
}
~A(){//析构
if (buf != nullptr)
{
delete[]buf;
cout << "~A" << endl;
}
}
A(const A &tmp)//拷贝构造函数 //= delete;
{
if(this!=&tmp)//检查自己拷贝自己
{
this->a = tmp.a;
this->buf = new int[100];
cout << "copy A" << endl;
}
}
A( A &&tmp)//移动构造
{
cout << "move A" << endl;
this->a = tmp.a;
this->buf = tmp.buf;
tmp.buf = nullptr;
}
A& operator=(const A &temp)//赋值函数 // = delete;
{
this->a = temp.a;
this->buf = new int[100];
cout << "= A" << endl;
return *this;
}
A operator=( A &&temp)//移动赋值
{
cout << "move = A" << endl;
this->a = temp.a;
this->buf = temp.buf;
temp.buf = nullptr; // 置为空指针
return std::move(*this);//变量强制转右值
}
A copyA(A&a)
{
return std::move(a);
}
};
int main()
{
list<A> list_;
for (auto a : { 1, 2, 4 })
{
cout << "-------begin------" << endl << endl;
list_.push_back(A());
}
cout << "-------1------" << endl;
list_.clear();
cout << "-------2------" << endl;
{
A a;
A b = a;
const A c;
A d = c;
}
getc(stdin);
return 1;
}
输出
-------begin------
A
move A
-------begin------
A
move A
-------begin------
A
move A
-------1------
~A
~A
~A
-------2------
A
copy A
A
copy A
~A
~A
~A
~A
智能指针
#include<iostream>
#include<list>
#include<memory>
using namespace std;
int cnt = 0; //记录C的分配析构次数
class C
{
public:
int c;
public:
C(int aa) :c(aa)
{
cnt++;
cout << "C" << endl;
}
~C()
{
cnt--;
cout << "~C" << endl;
}
C(const C&)
{
cout << "C copy" << endl;
cnt++;
}
};
int a = 0; //记录B的分配析构次数
class B
{
public:
std::shared_ptr<C> buf = nullptr;
public:
B() {
buf = make_shared<C>(3);
cout << "B" << endl;
a++;
}
~B() {
if (buf != nullptr)
{
a--;
cout << "~B" << endl;
}
}
B(const B& tmp) //= delete;
{
this->buf = make_shared<C>(*tmp.buf);
a++;
cout << "copy B" << endl;
//return *this;
}
B(B&& tmp)
{
cout << "move B" << endl;
this->buf = tmp.buf;
tmp.buf = nullptr;
}
B& operator=(const B& temp)// = delete;
{
a++;
this->buf = make_shared<C>(*temp.buf);
cout << "= B" << endl;
return *this;
}
B&& operator=(B&& temp)
{
cout << "move = B" << endl;
this->buf = temp.buf;
temp.buf = nullptr;
return std::move(*this);
}
};
int main()
{
list<B> list_;
for (auto a : { 1, 2, 4 })
{
cout << "-------begin------" << endl << endl;
list_.push_back(B());
}
cout << "-------1------" << endl;
list_.clear();
cout << "-------2------" << endl;
{
B a;
B b = a;
}
cout << "a = " << a << "cnt = " << cnt << endl;
getc(stdin);
return 1;
}
结果:
-------begin------
C
B
move B
-------begin------
C
B
move B
-------begin------
C
B
move B
-------1------
~B
~C
~B
~C
~B
~C
-------2------
C
B
C copy
copy B
~B
~C
~B
~C
a = 0cnt = 0
进程间通信
消息(Message)队列:消息队列是消息链式队列,消息被读完就删除,可以供多个进程间通信。有足够权限的进程可以向队列中添加消息,被赋予读权限的进程则可以读走队列中的消息。消息队列克服了信号承载信息量少,管道只能承载无格式字节流以及缓冲区大小受限等缺点。
共享内存:使得多个进程可以访问同一块内存空间,是最快的可用IPC形式。
/**
* @file share-memory.h
* @brief 进程间通信-共享内存头文件封装
* 共享内存创建之后,一直存在于内核中,读完之后,内容还存在,直到被删除或系统关闭
* @author yanjingang
*/
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <unistd.h>
#include <iostream>
#define PATHNAME "."
#define PROCID 'b' //0x6666
#define MEMSIZE 4096*1
using namespace