If a class has a data member that represents an association with another class that has cardinality 1, that data member should be a reference if possible. This to reduce null pointer access violations. This rule does not apply when objects have an association with each other with both a cardinality of '1'. In this case, only the object that is created last and also destroyed first, could use a reference instead of a pointer. For example:
class CMyFileClass
{
public:
CMyFileClass:: CMyFileClass(
const CMyDirectoryClass& rDirectory);
private:
CMyDirectoryClass& m_rDirectory;
};
CMyFileClass::CMyFileClass(const CMyDirectoryClass& rDirectory)
m_rDirectory(rDirectory)
{
...
}
|