Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of the return value of T* in C++

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "what is the role of the return value of T* in C++". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

F.42 T* return value should only be used to indicate the location Reason (reason)

That's what pointers are good for. Returning a T* to transfer ownership is a misuse.

This is the strength of pointers. Using T* to return ownership is the wrong use.

Example (sample)

Node* find (Node* t, const string& s) / / find s ina binary tree of Nodes {if (! t | | t-> name = = s) return t; if ((auto p = find (t-> left, s) return p; if ((auto p = find (t-> right, s)) return p; return nullptr;}

If it isn't the nullptr, the pointer returned by find indicates a Node holding s. Importantly, that does not imply a transfer of ownership of the pointed-to object to the caller.

If the return value is not nullptr, the pointer returned by find represents the Node that holds s. Importantly, it does not contain the meaning of passing ownership of the object pointed to by the pointer to the caller.

Note (Note)

Positions can also be transferred by iterators, indices, and references. A reference is often a superior alternative to a pointer if there is no need to use nullptr or if the object referred to should not change.

Locations can also be passed through iterators, indexes, and references. If you don't need to use nullptr or the object doesn't want to be modified, references are usually a better choice than pointers.

Note (Note)

Do not return a pointer to something that is not in the caller's scope; see F.43.

Do not return a pointer to something that is not within the scope of the caller.

Enforcement (implementation recommendations)

Flag delete, std::free (), etc. Applied to a plain tipped. Only owners should be deleted.

Indicates operations such as delete,std::free () that are applied to the direct pointer. Only the owner can delete.

Flag new, malloc (), etc. Assigned to a plain tipped. Only owners should be responsible for deletion.

Tag processes such as new,malloc (), which assigns the result to a direct pointer. Only the owner is responsible for destruction.

This is the end of the content of "what is the function of the return value of T* in C++"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report