扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
int daysHeld(const Investment *pi); // return number of days // investment has been held |
int days = daysHeld(pInv); // error! |
int days = daysHeld(pInv.get()); // fine, passes the raw pointer // in pInv to daysHeld |
class Investment { // root class for a hierarchy public: // of investment types bool isTaxFree() const; ... }; Investment* createInvestment(); // factory function std::tr1::shared_ptr<Investment> // have tr1::shared_ptr pi1(createInvestment()); // manage a resource bool taxable1 = !(pi1->isTaxFree()); // access resource // via operator-> ... std::auto_ptr<Investment> pi2(createInvestment()); // have auto_ptr // manage a // resource bool taxable2 = !((*pi2).isTaxFree()); // access resource // via operator* ... |
FontHandle getFont(); // from C API-params omitted // for simplicity void releaseFont(FontHandle fh); // from the same C API class Font { // RAII class public: explicit Font(FontHandle fh) // acquire resource; : f(fh) // use pass-by-value, because the {} // C API does ~Font() { releaseFont(f); } // release resource private: FontHandle f; // the raw font resource }; |
class Font { public: ... FontHandle get() const { return f; } // explicit conversion function ... }; |
void changeFontSize(FontHandle f, int newSize); // from the C API Font f(getFont()); int newFontSize; ... changeFontSize(f.get(), newFontSize); // explicitly convert // Font to FontHandle |
class Font { public: ... operator FontHandle() const { return f; } // implicit conversion function ... }; |
Font f(getFont()); int newFontSize; ... changeFontSize(f, newFontSize); // implicitly convert Font // to FontHandle |
Font f1(getFont()); ... FontHandle f2 = f1; // oops! meant to copy a Font // object, but instead implicitly // converted f1 into its underlying // FontHandle, then copied that |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者