TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C++ Coding Standard
Search

Rule:  OOP#005

Synopsis:Do use references for associations with cardinality '1' whenever possible
Language:C++
Severity Level:9
Category:Object Oriented Programming


Description:

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)
{
    ...
}