class Investment {...};
class Stock: public Investment {...};
class Bond: public Investment {...};
class RealEstate: public Investment {...};
template<typename... Ts>
std::unique_ptr<Investment> makeInvestment(Ts&&... params);
//using
{
...
auto pInvestment = makeInvestment(arguments);
...
}
auto delInvmt = [](Investment* pInvestment){
//此处使用基类指针来释放对象,为了正确释放实际对象类型的内存,
需要将基类的析构函数设为虚函数
makeLogEntry(pInvestment);
delete pInvestment;
};
template<typename... Ts>
std::unique_ptr<Investment, decltype<delInvmt>
makeInvestment(Ts&&... params)
{
std::unique_ptr<Investment, decltype(delInvmt)>
pInv(nullptr, delInvmt);
if(...)
pInv.reset(new Stock(std::forward<Ts>(params)...));
else if(...)
pInv.reset(new Bond(std::forward<Ts>(params)...));
else if(...)
pInv.reset(new RealEstate(std::forward<Ts>(params)...));
return pInv;
}
auto delInvmt1 = [](Invest* pInvestment){
makeLogEntry(pInvestment);
delete pInvestment;
};
template<typename... Ts>
std::unique_ptr<Investment, decltype(delInvmt1)>
makeInvestment(Ts&&... args);
// return type has size of Investment*
void delInvmt2(Investment* pInvestment)
{
makeLogEntry(pInvestment);
delete pInvestment;
}
template<typename... Ts>
std::unique_ptr<Investment, void (* )(Investment*)>
makeInvestment(Ts&&... params);
//return type has sizeof(Investment*)+least sizeof(function pointer)
auto loggingDel = [](Widget* pw) {
makeLogEntry(pw);
delete pw;
};
//自定义析构器是指针的一部分
std::unique_ptr<Widget, decltype(loggingDel)> upw(new Widget, loggingDel);
//自定义析构器不是指针的一部分
std::shared_ptr<Widget> spw(new Widget, loggingDel);
auto customDeleter1 = [](Widget* pw){...};
auto customDeleter2 = [](Widget* pw){...};
std::shared_ptr<Widget> pw1(new Widget, customDeleter1);
std::shared_ptr<Widget> pw2(new Widget, customDeleter2);
//带有不同自定义析构器的同类型std::shared_ptr可以被放在同一个容器中
std::vector<std::shared_ptr<Widget>> vpw{pw1, pw2};
auto pw = new Widget;
...
std::shared_ptr<Widget> spw1 (pw, loggingDel);
...
std::shared_ptr<Widget> spw2 (pw, loggingDel);
std::shared_ptr<Widget> spw1(new Widget, loggingDel);
std::shared_ptr<Widget> spw2(spw1);
std::vector<std::shared_ptr<Widget>> processedWidgets;
class Widget {
public:
...
void process();
...
};
void Widget::process()
{
...
processWidgets.emplace_back(this);
}
class Widget: public std::enable_shared_from_this<Widget> {
public:
...
void process();
...
};
void Widget::process()
{
...
processedWidgets.emplace_back(shared_from_this());
}
class Widget: public std::enable_shared_from_this<Widget> {
public:
template<typename... Ts>
static std::shared_ptr<Widget> create(Ts&&... params);
...
void process();
...
private:
Widget();
...
};
auto spw = std::make_shared<Widget>();
...
std::weak_ptr<Widget> wpw(spw);
...
spw = nullptr;
if(wpw.expired())
...
std::shared_ptr<Widget> spw2(wpw);
std::shared_ptr<const Widget> loadWidget(WidgetID id);
std::shared_ptr<const Widget> fastLoadWidget(WidgetID id)
{
static std::unordered_map<WidgetID, std::weak_ptr<const widget>> cache;
auto objPtr = cache[id].lock();
if(!objPtr){
objPtr = loadWidget(id);
cache[id] = objPtr;
}
return objPtr;
}
template<typename T, typename... Ts>
std::unique_ptr<T> make_unique(Ts&&... params)
{
return std::unique_ptr<T>(new T(std::forward<Ts>(params)...));
}
auto upw1(std::make_unique<Widget>()); //减少了一次Widget的出现
std::unique_ptr<Widget> upw1(new Widget);
auto spw2(std::make_shared<Widget>());
std::shared_ptr<Widget> spw2(new Widget);
void processWidget(std::shared_ptr<Widget> spw, int priority);
int computePriority();
processWidget(std::shared_ptr<Widget>(new Widget), computePriority());
processWidget(std::make_shared<Widget>(), computePriority());
std::shared_ptr<Widget> spw(new Widget);
auto spw = std::make_shared<Widget>();
auto initList = {10, 20};
auto spv = std::make_shared<std::vector<int>>(initList);
void processWidget(std::shared_ptr<Widget> spw, int priority);
void cusDel(Widget* ptr);
processWidget(std::shared_ptr<Widget>(new Widget, cusDel), computePriority());
// memory leak!! 右值ptr
std::shared_ptr<Widget> spw(new Widget, cusDel);
processWidget(spw, computePriority()); //spw左值
processWidget(std::move(spw), computePriority());
# widget.h
class Widget {
public:
Widget();
~Widget();
...
private:
struct Impl;
Impl* pImpl;
};
# widget.cpp
#include"widget.h"
#include"gadget.h"
#include<string>
#include<vector>
struct Widget::Impl{
std::string name;
std::vector<double> data;
Gadget g1, g2, g3;
};
Widget::Widget(): pImpl(new Impl) {}
Widget::~Widget() {delete pImpl; }
# widget.h
class Widget {
public:
Widget();
~Widget();
...
private:
struct Impl;
std::unique_ptr<Impl> pImpl;
};
# widget.cpp
#include"widget.h"
#include"gadget.h"
#include<string>
#include<vector>
struct Widget::Impl{
std::string name;
std::vector<double> data;
Gadget g1, g2, g3;
};
Widget::Widget(): pImpl(std::make_unique<Impl>()) {}
#include "widget.h"
Widget w; // error!!!!!!
# widget.h
class Widget {
public:
Widget();
~Widget();
...
private:
struct Impl;
std::unique_ptr<Impl> pImpl;
};
# widget.cpp
#include"widget.h"
#include"gadget.h"
#include<string>
#include<vector>
struct Widget::Impl{
std::string name;
std::vector<double> data;
Gadget g1, g2, g3;
};
Widget::Widget(): pImpl(std::make_unique<Impl>()) {}
Widget::~Widget() { }
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有