wrote:
>
> Jacob wrote:
>
>>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:
>
>
> This is not hard. You simply comment the member variable as "owned" or
> "held".
I like to document as much as possible with the syntax
of the language. Relaying on comment conventions like this
is asking for trouble down the road.
>>Case 2: When a secondary object exists somewhere else (i.e. is owned
>>by someone else) but is referred to by the class.
>
>
> This is where things get interesting. When you give an external object
> to a class to hold, always pass it as a pointer:
I see no reason why this should not be a reference.
This is after all the classical situation where you
do use a reference, isn't it?
> Passing by const reference is, by convention, an indication that the
> argument may be copied by the other object, and the caller is free to
> destroy the argument object at any point relative to the other object:
You claim that parameters passed as const reference should
always be copied at the destination? That is not my current
practice, could you elaborate?
public:
void setMother(const Person& mother)
{
mother_ = mother;
}
private:
Person mother_;
I'd rather keep the private variable as a reference, but it is
of course then assumed that the object (the mother) outlive
the specific instance.