网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C++怎么使用T*或onwer<T*>指明唯一对象

这篇文章主要介绍“C++怎么使用T*或onwer指明唯一对象”,在日常操作中,相信很多人在C++怎么使用T*或onwer指明唯一对象问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++怎么使用T*或onwer指明唯一对象”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

我们提供的服务有:网站设计制作、做网站、微信公众号开发、网站优化、网站认证、四平ssl等。为上千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的四平网站制作公司

F.22: Use T* or owner to designate a single object(使用T*或owner指明唯一对象)

Reason(原因)

Readability: it makes the meaning of a plain pointer clear. Enables significant tool support.

可读性:这可以让裸指针的含义更明确。使重要的工具支持有效。

译者注:owner是gsl(准则支持库)提供的一个功能,从编译的角度来看和T*的含义一致,但是附加了所有权语义,可以帮助程序员理解代码和工具检查。

Note(注意)

In traditional C and C++ code, plain T* is used for many weakly-related purposes, such as:

在传统的C和C++代码中,裸指针用于很多没有什么关系的目的,例如:

  • Identify a (single) object (not to be deleted by this function)

    表示一个(单一)对象(不会被本函数删除)

  • Point to an object allocated on the free store (and delete it later)

    指向一个从自由存储上获取的对象(以后会删除)

  • Hold the nullptr

    持有nullptr

  • Identify a C-style string (zero-terminated array of characters)

    表示一个C风格字符串(以0结尾的字符数组)

  • Identify an array with a length specified separately

    表示一个数组,长度被另外定义

  • Identify a location in an array

    表示数组的首地址

This makes it hard to understand what the code does and is supposed to do. It complicates checking and tool support.

这样做的结果是很难理解代码在做什么和被预期做什么。无论是检查还是工具支持都会复杂。

Example(示例)

void use(int* p, int n, char* s, int* q){    p[n - 1] = 666; // Bad: we don't know if p points to n elements;                    // assume it does not or use span    cout << s;      // Bad: we don't know if that s points to a zero-terminated array of char;                    // assume it does not or use zstring    delete q;       // Bad: we don't know if *q is allocated on the free store;                    // assume it does not or use owner}

译者注:这可能是我们每天都能见到的指针用法,很难看出它们到底是前面提供的那种用法。

better(稍好)

void use2(span p, zstring s, owner q){    p[p.size() - 1] = 666; // OK, a range error can be caught    cout << s; // OK    delete q;  // OK}

Note(注意)

owner represents ownership, zstring represents a C-style string.

owner表现所有权,zstring表达C风格的字符串

Enforcement(实施建议)

  • (Simple) ((Bounds)) Warn for any arithmetic operation on an expression of pointer type that results in a value of pointer type.

    (简单)((边界))警告所有针对指针表达式,并得到指针类型结果的算数操作。

到此,关于“C++怎么使用T*或onwer指明唯一对象”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


当前名称:C++怎么使用T*或onwer<T*>指明唯一对象
文章起源:http://bjjierui.cn/article/jgdjsh.html

其他资讯