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_ptr
Provides a get
way to get resources.
shared_ptr<Investment> PINV;void Daysheld(Investment *Pi);int Days = Daysheld(PINV.Get());
In order to letpInv
Behaves more like a pointer,shared_ptr
Also 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
Provideget
Methodoperator->
、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 providesFontHandle
To 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, theFont
Converted toFontHandle
, then acceptFontHandle
type function as a parameter will accept the sameFont
Type. 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