Item 15: Resource management classes need to provide access to raw resources effective C + + notes

Source: Internet
Author: User

Item 15:provide access to raw resources in resource-managing classes.

In a perfect design, all resource access should be done through resource management objects, and resource leaks are perfectly overcome. However, the world is imperfect, and many APIs directly manipulate resources, especially some C-language APIs. In short, you will occasionally find it necessary to access resources directly, so the resource management object needs to provide access to the original resources. There are two types of ways to get resources: implicit and explicit. In general, explicit resource acquisition is better, minimizing the chance of unintentional type conversions .

To obtain resources explicitly

shared_ptrProvides a get way to get resources.

shared_ptr<Investment> PINV;void Daysheld(Investment *Pi);int  Days = Daysheld(PINV.Get());

In order to letpInvBehaves more like a pointer,shared_ptrAlso overloaded understanding reference operators (dereferencing operator)operator->Andoperator*

class Investment{ Public:     BOOL Istaxfree() Const;};shared_ptr<Investment> pi1(createinvestment());BOOL Taxable1 = !(pi1 -Istaxfree());BOOL Texable2 = !((*pi1).Istaxfree());
Get Resources implicitly

ProvidegetMethodoperator->operator*has made it easy to access resources. Unfortunately, programmers are lazy, and we want to be easier. Implicit conversion operators can do this, such as the operating system providesFontHandleTo manipulate fonts:

fonthandle  getfont   ();  void  releasefont   ( fonthandle  fh  Span class= "P" style= "" "); void  changefontsize   ( fonthandle  f  Span class= "P" style= "" ", int  newsize   

We encapsulate the Font resources to manage:

class Font{Fonthandle F; Public:    Explicit Font(Fonthandle FH): F(FH){}    ~Font(){ Releasefont(F); };    Fonthandle Get() Const { return F; }};

through get method to access fonthandle :

Font f(getFont());int newFontSize;changeFontSize(f.get(), newFontSize);

If an implicit type conversion operator is provided, theFontConverted toFontHandle, then acceptFontHandletype function as a parameter will accept the sameFontType. Everything will be easier:

class  font  { operator  fonthandle   ()  Span class= "K" style= "Color:rgb (0,0,255)" >const { return  f  ;}  };  changefontsize   ( f  ,  newfontsize   

However, the problem also arises:

FontHandleh2=f1;

Users accidentally copied a copy of a resource! The resource is not managed. This will cause an unexpected resource leak. So implicit conversion, while providing convenience, also raises the risk of resource leakage. When considering whether to provide implicit conversions, it is necessary to weigh the design intent of the resource management class and its specific usage scenarios. In general, explicit resource acquisition is better, minimizing the chance of unintentional type conversions.

Unless noted, this blog article is original, reproduced please link to the form of this article address: http://harttle.com/2015/08/05/effective-cpp-15.html

Copyright NOTICE: This article is for bloggers original articles, reproduced please attach the original link.

Item 15: Resource management classes need to provide access to raw resources effective C + + notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.