博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++11实现模板手柄:委托构造函数、defaultkeyword分析
阅读量:6941 次
发布时间:2019-06-27

本文共 3027 字,大约阅读时间需要 10 分钟。

C++11。使用委托构造函数。和高速变量初始化,defaultkeyword重新声明默认构造函数,回答pod状态。

分析与推荐的方法。

到目前为止,VS2012和2013异常声明兼容还是停留在通信代码级,查,出现例如以下错误可忽略。

warning C4290: 忽略 C++ 异常规范,但指示函数不是 __declspec(nothrow)

下为:VS2012不支持托付构造函数。建议使用cocos2d-x 3.2及版本号的朋友更新VS至2013版。

1>d:\cpp_lab\testqueue_16th_2\testqueue_16th_2\handle.hpp(9): error C2614: “Handle<T>”: 非法的成员初始化:“Handle<int>”不是基或成员

同一时候VS2012也不支持高速初始化变量。

VS2013同一时候完毕了对于default声明。可是假设考虑和委派构造函数一起使用的话,则会有例如以下的错误:

Handle() = default:Handle(p,new std::size_t(1)){};

1>d:\cpp_2013\testqueue_16th_2\testqueue_16th_2\handle.hpp(7): error C2143: 语法错误 : 缺少“;”(在“:”的前面)
编译时出现语义上的二义性。要又一次定义=():()形式的语义?

原因在:一旦使用defaultkeyword去重定义默认的构造函数使其回复至pod状态,要记得不能定义函数体,默认构造函数是由编译器合成的。

若定义了default构造函数的函数体,则会出现重载定义无法识别的编译报错。

VS2013版源代码例如以下:

class Handle{public:	Handle() = default; // 不能声明函数体	Handle(T *p = nullptr):Handle(p,new std::size_t(1))	{	}	Handle(const Handle & h):Handle(h.m_pPtr,h.m_pUseCount)//托付构造函数,将先调用目标构造函数,再完毕初始化	{			}	Handle & operator = (const Handle & other);	T & operator *() throw (std::runtime_error);	T * operator ->() throw (std::runtime_error);	T & operator *() const throw (std::runtime_error);	T * operator ->() const throw (std::runtime_error);	~Handle(void)	{		rem_ref();	}private:	Handle(T*p,size_t * use):m_pPtr(p),m_pUseCount(use) // 目标构造函数	{		if (m_pPtr==nullptr)		{			 delete m_pUseCount;			 m_pUseCount = nullptr;		}
				else
				{
					++(*m_pUseCount);
				}
};	void rem_ref()	{		if (--*m_pUseCount == 0)		{			delete m_pPtr;			delete m_pUseCount;			m_pPtr = nullptr;			m_pUseCount = nullptr;		}	}private:	T * m_pPtr = nullptr;//实际上这个赋值没太多意思	size_t * m_pUseCount = nullptr;};template 
Handle
& Handle
::operator = (const Handle
& other){ ++other.m_pUseCount; rem_ref(); m_pPtr = other.m_pPtr; m_pUseCount = other.m_pUseCount; return *this;}template
inline T & Handle
::operator *(){ if (m_pPtr!=nullptr) { return *m_pPtr; } else { throw std::runtime_error("dereference of unbound Handle"); } }template
inline T * Handle
::operator ->(){ if (m_pPtr!=nullptr) { return m_pPtr; } else { throw std::runtime_error("dereference of unbound Handle"); }} template
inline T & Handle
::operator *() const{ if (m_pPtr!=nullptr) { return *m_pPtr; } else { throw std::runtime_error("dereference of unbound Handle"); }}template
inline T * Handle
::operator ->() const{ if (m_pPtr!=nullptr) { return m_pPtr; } else { throw std::runtime_error("dereference of unbound Handle"); }}
main函数:

int*p = new int(100);	Handle
pInt(p); Handle
pInt2 = Handle
(pInt); Handle
pInt3 = pInt2; try { cout << *pInt << "\t" << *pInt2 << "\t" << *pInt3 << endl; } catch (runtime_error e) { cerr<
<
委派构造函数的使用在于内部能定义一个完整版本号的构造函数,然后在类对象未完毕初始化时调用该构造函数,先在当前地址完毕构造。这个思维应该是曾经this为很量时代(事实上我没用过很量指针形式的this)的使用方法,后来新标准定义this仅仅能为const类型,融合当前代码时长出现大量的构造函数。思考出的解决方式,不算是语法上的突破或革新。

程序结果:

版权声明:本文博主原创文章。博客,未经同意不得转载。

你可能感兴趣的文章
xshell 登陆堡垒机实现自动跳转
查看>>
Hexo-设置阅读全文
查看>>
实模式与保护模式
查看>>
分布式ID生成器解决方案
查看>>
ResolveUrl in external JavaScript file in asp.net project
查看>>
EL表达式JSON应用
查看>>
人民邮电出版社图灵公司征求《Windows Communication Foundation Unleashed》译者
查看>>
使用pidstat查看进程资源使用情况
查看>>
PatternsInJava文摘
查看>>
理解SVN关键词BASE,HEAD,COMMITTED,PREV
查看>>
AOP 实现的原理简析
查看>>
linux命令汇总1
查看>>
PHP 初学
查看>>
I.MX6 U-boot编译找不到用户目录
查看>>
date 修改系统时间
查看>>
python coroutine的学习跟总结[转]
查看>>
String 的扩展方法
查看>>
[zhuan]Simple Emacs Configuration
查看>>
Flex Builder 3 下载与注册
查看>>
【存储方式】SharedPreference
查看>>