栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++语言中的一些小点

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++语言中的一些小点

NULL和nullptr的区别
#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;//无法将左值绑定到右值引用上

	
	const int &c = 20;

	
	int &&d = 20;//可以把右值绑定到右值引用上

	//int &&f = d;//一个右值引用变量,本身是一个左值,无法绑定到右值引用上
	int &f = d;
	
	return 0;
}
auto_ptr
auto_ptr ptr1(new int);
auto_ptr 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&> = delete;
scoped_ptr& operator=(const scoped_ptr&) = delete;
unique_ptr
	

	unique_ptr p1(new int);
	unique_ptr p2(std::move(p1));
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/920427.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号