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

Rule:  OLC#020Checked automatically with code checker

Synopsis:Don't pass member variables of type "class" by reference to the base class in the constructor's initializer list
Language:C++
Severity Level:2
Category:Object Life Cycle


Description:

Because the base class is created before the member variables, these members are not created/initialized yet in the constructor of the base class. If the constructor of the base class uses one of the parameters, an access violation error is inevitable when this constructor is called.

Example:

class Base
{
public:
    Base(DMember& m)
    {
        m.Test();      // Access violation!!!
    }
};

class Derived: public Base
{
public:
    Derived()
    : Base(m_Member)
    {
    }
private:
    DMember m_Member;   // Class DMember contains a Test method which uses DMember data
}

If an object of Derived is created, then first Base is created with the not-constructed m_Member as input argument. When the Base constructor calls the Test method, an access violation exception will occur.