Skip to content

nullptr initialization of pointer member variables in class definition #223

Open
@dreamer2368

Description

@dreamer2368

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(...)).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions