Better practice for heap object getters / setters in C++
问题 I'm currently having Type1 &GetType1() const { return *this->type1; } void SetType1(const Type1 &type1) { *this->type1 = type1; } and in the class definition class Type2 { public: Type2(); virtual ~Type2(); Type1 &GetType1() const; void SetType1(const Type1 &type1); private: Type1 *type1 = nullptr; } And in main int main() { Type2 *type2 = new Type2(); Type1 *newType1 = new Type1(); type2->SetType1(*newType1); delete type2; delete newType1; } everywhere in my project. It seems to me that this