
#include
using namespace std;
void func(int *x) {
cout << __PRETTY_FUNCTION__ << endl;
cout << x << endl;
}
void func(int x) {
cout << __PRETTY_FUNCTION__ << endl;
cout << x << endl;
}
int main() {
//func(NULL); //编译报错,因为NULL即代表0又代表空地址
func(nullptr); //nullptr代表空地址
return 0;
}
左值、右值
int main() {
int a = 10;
int &b = a; //左值:有内存、有名字 右值:没名字、没内存
//int &&c = a;//无法将左值绑定到右值引用上
/*
int tmp = 20;
const int &c = tmp;
*/
const int &c = 20;
/*
int tmp = 20;
int &&d = tmp;
*/
int &&d = 20;//可以把右值绑定到右值引用上
//int &&f = d;//一个右值引用变量,本身是一个左值,无法绑定到右值引用上
int &f = d;
return 0;
}
auto_ptr
auto_ptr<int> ptr1(new int);
auto_ptr<int> ptr2(ptr1);
*ptr2 = 20;
cout << *ptr1 << endl;//非法
ptr1拷贝给ptr2时,ptr1不再指向原来的地址
//auto_ptr源码
auto_ptr(_Myt& _Right) _THROW0()
: _Myptr(_Right.release())
{ // construct by assuming pointer from _Right auto_ptr
}
_Ty *release() _THROW0()
{ // return wrapped pointer and give up ownership
_Ty *_Tmp = _Myptr;
_Myptr = 0;
return (_Tmp);
}
scoped_ptr
scoped_ptr<const scoped_ptr<T>&> = delete;
scoped_ptr<T>& operator=(const scoped_ptr<T>&) = delete;
unique_ptr
/*
unique_ptr(const unique_ptr&) = delete;
unique_ptr& operator=(const unique_ptr&) = delete;
unique_ptr(unique_ptr &&src);
unique_ptr(T)& operator=(unique_ptr &&src)
tempate
unique_ptr getSmartPtr()
{
unique_ptr ptr(new T());
return ptr;
}
unique_ptr ptr1 = getSmartPtr();
ptr1 = getSmartPtr();
*/
unique_ptr<int> p1(new int);
unique_ptr<int> p2(std::move(p1));
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)