十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
class widget
{public:
widget();
~widget();
private:
std::string name;
std::vectordata;
};
#pragma once
class widget
{public:
widget();
~widget();
private:
struct Impl;
struct Impl *pImpl;
};
#include "widget.h"
#include#include#include "Gadget.h"
struct widget::Impl {std::string name;
std::vectordata;
Gadget g;
};
widget::widget()
:pImpl(new Impl())
{}
widget::~widget()
{delete pImpl;
}
#pragma once
#includeclass widget
{public:
widget();
~widget();
widget(const widget& rhs);
widget &operator=(const widget& rhs);
widget(widget &&rhs);
widget &operator=(widget &&rhs);
private:
struct Impl;
std::unique_ptrpImpl;
};
#include "widget.h"
#include#include#include "Gadget.h"
struct widget::Impl {std::string name;
std::vectordata;
Gadget g;
};
widget::widget()
:pImpl(std::make_unique())
{}
widget::~widget() = default;
widget::widget(const widget & rhs)
:pImpl(std::make_unique(*rhs.pImpl))
{}
widget &widget::operator=(const widget& rhs)
{*pImpl = *rhs.pImpl;
return *this;
}
widget::widget(widget && rhs) = default;
widget &widget::operator=(widget &&rhs) = default;
在不進行資源共享的前提下,為達到實現(xiàn)PImpl慣用法的目的,應(yīng)該選擇使用std::unique_ptr智能指針,因為對象內(nèi)部的pImpl指針擁有相應(yīng)實現(xiàn)對象的專屬所有權(quán)。(含義清晰,但是編碼復雜)
#pragma once
#includeclass widget
{public:
widget();
widget(const widget& rhs);
widget &operator=(const widget& rhs);
private:
struct Impl;
std::shared_ptrpImpl;
};
#include "widget.h"
#include#include#include "Gadget.h"
struct widget::Impl {std::string name;
std::vectordata;
Gadget g;
};
widget::widget()
:pImpl(std::make_shared())
{}
widget::widget(const widget & rhs)
:pImpl(std::make_shared(*rhs.pImpl))
{}
widget &widget::operator=(const widget& rhs)
{*pImpl = *rhs.pImpl;
return *this;
}
為什么同樣是智能指針卻有如此大的差別?你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧