私は C++ にはかなり慣れていませんが (C や OOP ではありません)、物事を「正しい」方法で行い、悪い習慣や危険な習慣を避けようとしているので、Google の C++ コーディング ガイドラインと効果的な C++ を次のように使用しています。私の学びの出発点。
unique_ptr
メンバーとして aを持つ抽象基本クラスがあります。非公開にし、getter を介して派生クラスへのアクセスのみを提供する場合 (Google C++ スタイル ガイドラインに従って)、これが最善の方法ですか? それとも、ここで潜在的な落とし穴を見逃していますか?
Base.h:
#include "Document.h"
typedef std::unique_ptr<Document> pDOC;
class Base {
public:
Base();
virtual ~Base() = 0;
pDOC& GetDoc();
private:
// Unique pointers cannot be shared, so don't allow copying
Base(Base const &); // not supported
Base &operator=(Base const &); // not supported
pDOC m_doc;
};
Base.cpp
#include "base.h"
Base::Base()
: m_xmldoc(new Document) {}
// Class destructor (no need to delete m_doc since it is a smart pointer)
Base::~Base() {}
pDOC& Base::GetDoc() {
return m_doc;
}