mgmol icon indicating copy to clipboard operation
mgmol copied to clipboard

`nullptr` initialization of pointer member variables in class definition

Open dreamer2368 opened this issue 10 months ago • 0 comments

Currently, mgmol classes do not initialize pointer member variables in their class definitions. For example of MGMol<OrbitalsType>,

template <class OrbitalsType>
class MGmol : public MGmolInterface
{
private:
    std::ostream& os_;

    MPI_Comm comm_;

    XConGrid* xcongrid_;                        // <--

    OrbitalsType* current_orbitals_;            // <--

    AOMMprojector* aomm_;                       // <--

    Ions* ions_;                                // <--

    ....

This is very error-prone and hard to debug, as they have uninitialized pointers which exhibit unexpected behaviors. This practice should be changed as soon as possible, as below:

template <class OrbitalsType>
class MGmol : public MGmolInterface
{
private:
    std::ostream& os_;

    MPI_Comm comm_;

    XConGrid* xcongrid_ = nullptr;

    OrbitalsType* current_orbitals_ = nullptr;

    AOMMprojector* aomm_ = nullptr;

    Ions* ions_ = nullptr;

    ....

This is usually the safest and shortest initialization. An alternative is to define them as nullptr in the constructor (For example, MGmol<OrbitalsType>::MGmol(...)).

dreamer2368 avatar Apr 19 '24 19:04 dreamer2368