mardi 4 juin 2019

Should I initialize member values in pure virtual C++ class?

I'd like to know if it is best practice to initialize member variables in an abstract class. The other choice is to do it in the inheriting classes.

Initializing in inheriting class

class IPlayer
{
public:
        virtual ~IPlayer() {};

protected:
        bool m_alive;
};

class Bomber : protected IPlayer
{
public:
        Bomb(bool t_alive = true)
        {
                m_alive = t_alive;
        }
        ~Bomb();
};

Initializing in parent, abstract class

class IPlayer
{
public:
        virtual ~IPlayer() {};

protected:
        bool m_alive { true };
};

class Bomber : protected IPlayer
{
public:
        Bomb();
        ~Bomb();
};


Which one is better?

Aucun commentaire:

Enregistrer un commentaire