i'm having
type1 &gettype1() const { return *this->type1; } void settype1(const type1 &type1) { *this->type1 = type1; }
and in 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 project. seems me not safe method, there cases in object pointing null, etc.. know if there better commonly accepted way that. maybe opperation overloading idea?
if class has member pointer can null, return pointer getter function , have user worry corner cases.
type1* gettype1(){ return this->type1; } void settype1(type1* type1) { this->type1 = type1; }
if, chance member cannot ever become null, class invariant, think practice return reference, , use assertion in getter.
Comments
Post a Comment