面试让我手撕unique_ptr,被吓怕了,记录一些偏底层的实现或者一些可能让手撕的比较偏的函数
- unique_ptr实现
- memcpy实现—注意地址重叠
- 重写构造函数与赋值函数—有指针类型时,防止重复释放内存
- 可变参数模板
- i++,++i
- shared_ptr死锁情况
- 单例模式
- 工厂模式(简单工厂模式、工厂模式、抽象工厂模式)
unique_ptr实现
#include <iostream>
using namespace std;
template<class T>
class Unique_ptr
{
private:
T* ptr=NULL;
public:
Unique_ptr(Unique_ptr& u) = delete;
Unique_ptr& operator=(Unique_ptr& u) = delete;
Unique_ptr(T* p)
{
ptr = p;
cout << "构造" << endl;
}
~Unique_ptr()
{
if (ptr)
{
delete ptr;
ptr = NULL;
cout << "析构" << endl;
}
}
Unique_ptr(Unique_ptr&& p)
{
ptr = p.ptr;
p.ptr = NULL;
}
T* operator->()
{
return ptr;
}
T& operator*()
{
if (ptr)
{
return *ptr;
}
T a = -1;
return a;
}
};
int main()
{
int* a = new int(10);
Unique_ptr<int> p(a);
cout << *p << endl;
Unique_ptr<int> p1=move(p);
cout << *p1 << endl;
cout << *p << endl;
}
memcpy实现—注意地址重叠
#include <iostream>
using namespace std;
template <class T>
T* my_memcpy(T* dst, T* src, int size_s)
{
if (sizeof(dst) < size_s)
{
return NULL;
}
if (dst == NULL || src == NULL)
{
return NULL;
}
if (src < dst && (src + size_s > dst))
{
T* d = dst + size_s - 1;
T* s = src + size_s - 1;
while (size_s--)
{
*d = *s;
d--;
s--;
}
}
else
{
T* d = dst;
T* s = src;
while (size_s--)
{
*d = *s;
d++;
s++;
}
}
return dst;
}
int main()
{
char buf[5] = "abcd";
char buf1[4];
cout << sizeof(buf1);
char* res=my_memcpy<char>(buf1, buf, 5);
if (res == NULL)
{
cout << 0;
return 0;
}
for (int i = 0; i < strlen(res); i++)
{
cout << res[i];
}
}
重写构造函数与赋值函数—有指针类型时,防止重复释放内存
#include <iostream>
using namespace std;
class base
{
private:
int id;
int* ptr;
public:
base(int a, int* d)
{
id = a;
ptr = new int;
if (d)
{
*ptr = *d;
}
}
~base()
{
if (ptr)
{
delete ptr;
ptr = NULL;
cout << "析构" << endl;
}
}
base(const base& b)
{
id = b.id;
ptr = new int;
if (b.ptr)
{
*ptr = *(b.ptr);
}
}
base& operator=(const base& b)
{
id = b.id;
ptr= new int;
if (b.ptr)
{
*ptr = *(b.ptr);
}
return *this;
}
void get()
{
cout << id << " " << *ptr << endl;
}
};
int main()
{
int* a = new int(10);
base b(2, a);
base c(b);
base d = c;
b.get() ;
c.get();
d.get();
}
可变参数模板
#include <iostream>
using namespace std;
template <typename T>
void foo( T& t)
{
cout << t<<endl;
}
template<class T,class ...Arg>
void foo(T i,Arg ...t)
{
cout << i << endl;
foo(t...);
}
int main()
{
foo(1,2,3,4);
std::cout << "Hello World!\n";
}
i++,++i
int& int::operator++()
{
*this += 1;
return *this;
}
const int int::operator++(int)
{
int oldValue = *this;
++(*this);
return oldValue;
}
shared_ptr死锁情况
#include <iostream>
using namespace std;
class B;
class A
{
public:
shared_ptr<B>_pb;
A()
{
cout << "A构造" << endl;
}
~A()
{
cout << "A析构" << endl;
}
};
class B
{
public:
shared_ptr<A>_pa;
B()
{
cout << "B构造" << endl;
}
~B()
{
cout << "B析构" << endl;
}
};
void fun()
{
shared_ptr<A> pa(new A);
shared_ptr<B> pb(new B);
pa->_pb = pb;
pb->_pa = pa;
cout << pa.use_count()<< endl;
}
int main()
{
fun();
return 0;
}
单例模式
#include <iostream>
using namespace std;
class Single
{
private:
Single() {};
Single& operator=(Single& s) = delete;
Single(Single& s) = delete;
static Single *s;
public:
~Single() {};
static Single* get()
{
if (s==nullptr)
{
s = new Single;
}
return s;
}
};
Single* Single::s = NULL;
class singleton_e {
private:
singleton_e() {}
singleton_e(const singleton_e&) = delete;
singleton_e& operator=(const singleton_e&) = delete;
static singleton_e* p;
public:
static singleton_e* instance() {
return p;
}
int a;
};
singleton_e* singleton_e::p = new singleton_e();
class Singleton_s
{
public:
~Singleton_s() {};
Singleton_s(const Singleton_s&) = delete;
Singleton_s& operator=(const Singleton_s&) = delete;
static Singleton_s& get_instance() {
static Singleton_s instance;
return instance;
}
private:
Singleton_s() {
std::cout << "constructor called!" << std::endl;
}
};
工厂模式(简单工厂模式、工厂模式、抽象工厂模式)
简单工厂模式
#include <iostream>
using namespace std;
class Product
{
public:
virtual void show() = 0;
};
class Product_A : public Product
{
public:
void show()
{
cout << "Product_A" << endl;
}
};
class Product_B : public Product
{
public:
void show()
{
cout << "Product_B" << endl;
}
};
class Factory
{
public:
Product* Create(int i)
{
switch (i)
{
case 1:
return new Product_A;
break;
case 2:
return new Product_B;
break;
default:
break;
}
}
};
int main()
{
Factory* factory = new Factory();
factory->Create(1)->show();
factory->Create(2)->show();
system("pause");
return 0;
}
工厂模式
#include <iostream>
using namespace std;
class Product
{
public:
virtual void show() = 0;
};
class Product_A : public Product
{
public:
void show()
{
cout << "Product_A" << endl;
}
};
class Product_B : public Product
{
public:
void show()
{
cout << "Product_B" << endl;
}
};
class Factory
{
public:
virtual Product* create() = 0;
};
class Factory_A : public Factory
{
public:
Product* create()
{
return new Product_A;
}
};
class Factory_B : public Factory
{
public:
Product* create()
{
return new Product_B;
}
};
int main()
{
Factory_A* productA = new Factory_A();
Factory_B* productB = new Factory_B();
productA->create()->show();
productB->create()->show();
system("pause");
return 0;
}
抽象工厂模式
#include <iostream>
using namespace std;
class product1
{
public:
virtual void show() = 0;
};
class product_A1 :public product1
{
public:
void show() { cout << "product A1" << endl; }
};
class product_B1 :public product1
{
public:
void show() { cout << "product B1" << endl; }
};
class product2
{
public:
virtual void show() = 0;
};
class product_A2 :public product2
{
public:
void show() { cout << "product A2" << endl; }
};
class product_B2 :public product2
{
public:
void show() { cout << "product B2" << endl; }
};
class Factory
{
public:
virtual product1* creat1() = 0;
virtual product2* creat2() = 0;
};
class FactoryA
{
public:
product1* creat1() { return new product_A1(); }
product2* creat2() { return new product_A2(); }
};
class FactoryB
{
public:
product1* creat1() { return new product_B1(); }
product2* creat2() { return new product_B2(); }
};
int main()
{
FactoryA* factoryA = new FactoryA();
factoryA->creat1()->show();
factoryA->creat2()->show();
FactoryB* factoryB = new FactoryB();
factoryB->creat1()->show();
factoryB->creat2()->show();
return 0;
}
|