J
Jacob
I am trying to find the best way of documenting (in code and
comments) ownership of secondary contained objects. This is my
current status, and I would appreciate feedback on it:
Case 1: When the secondary object is created with the object
and dies with the object.
Solution: Keep the secondary object as a stack variable, and
deallocation becomes a no-issue.
Example:
class Person
{
:
private:
Gender gender_;
};
Case 2: When a secondary object exists somewhere else (i.e. is owned
by someone else) but is referred to by the class.
Solution: Keep secondary object as a (possibly const) reference.
Pass reference through constructor or setter method.
The reference indicate that it exists somewhere else and the class
should never delete it.
Example:
class Person {
public:
Person(Person& mother);
private:
Person& mother_;
};
Case 3: Like case 2, but where the secondary object might be 0.
Solution: Keep secondary object as a (possibly const/const) pointer.
Pass pointer throgh constructor or setter method BUT DOCUMENT THE
FACT THAT OWNERSHIP IS ELSEWHERE.
Example:
class Person {
public:
void setSpouse(Person* spouse);
private:
Person* spouse_;
};
Case 4: A secondary object is created by a client and passed into
a class who becomes its owner.
Solution: Keep secondary object as a pointer internally and also
pass it as a pointer (even when it cannot be 0). Possibly document
the fact that the secondary object is now owned by the object, and
also document that it cannot be null by a precondition assert.
Example:
class Person {
public:
void setHairstyle(Hairstyle* hairstyle);
private:
Hairstyle* hairstyle_;
};
Person::~Person()
{
delete hairstyle_;
}
Currently I have trouble with case 3 where I need to document
(by comments) who owns the object, and case 4 where I pass an object
through a pointer even when it cannot be 0. I could technically pass
it as a reference but by passing it as a pointer I explicitly indicate
that it is *not* a reference to an object that exists elsewhere.
It then becomes (more) obvious where ownership is.
Any thoughts?
Thanks!
comments) ownership of secondary contained objects. This is my
current status, and I would appreciate feedback on it:
Case 1: When the secondary object is created with the object
and dies with the object.
Solution: Keep the secondary object as a stack variable, and
deallocation becomes a no-issue.
Example:
class Person
{
:
private:
Gender gender_;
};
Case 2: When a secondary object exists somewhere else (i.e. is owned
by someone else) but is referred to by the class.
Solution: Keep secondary object as a (possibly const) reference.
Pass reference through constructor or setter method.
The reference indicate that it exists somewhere else and the class
should never delete it.
Example:
class Person {
public:
Person(Person& mother);
private:
Person& mother_;
};
Case 3: Like case 2, but where the secondary object might be 0.
Solution: Keep secondary object as a (possibly const/const) pointer.
Pass pointer throgh constructor or setter method BUT DOCUMENT THE
FACT THAT OWNERSHIP IS ELSEWHERE.
Example:
class Person {
public:
void setSpouse(Person* spouse);
private:
Person* spouse_;
};
Case 4: A secondary object is created by a client and passed into
a class who becomes its owner.
Solution: Keep secondary object as a pointer internally and also
pass it as a pointer (even when it cannot be 0). Possibly document
the fact that the secondary object is now owned by the object, and
also document that it cannot be null by a precondition assert.
Example:
class Person {
public:
void setHairstyle(Hairstyle* hairstyle);
private:
Hairstyle* hairstyle_;
};
Person::~Person()
{
delete hairstyle_;
}
Currently I have trouble with case 3 where I need to document
(by comments) who owns the object, and case 4 where I pass an object
through a pointer even when it cannot be 0. I could technically pass
it as a reference but by passing it as a pointer I explicitly indicate
that it is *not* a reference to an object that exists elsewhere.
It then becomes (more) obvious where ownership is.
Any thoughts?
Thanks!