mercredi 1 juillet 2015

object construction : default parameter vs delegation

Consider the following code where I'm trying to introduce a default constructor as well as a parameterized one for class A. This way was introduced in recent c++ improvements.

class A  {
    private:
        unsigned int count;

    public:
        A(int new_c) : count(new_c) {}
        A() : A(10) {}
};

vs the old way of setting a default parameter on parameterized constructor and ignoring the default constructor completely.

class A  {
    private:
        unsigned int count;

    public:
        A(int new_c = 5) : count(new_c) {}
};

Is there any advantage using 1st method over the 2nd one apart from following modern conventions?

Aucun commentaire:

Enregistrer un commentaire