vendredi 1 septembre 2017

A member variable's initialization must be deferred. What is(are) the best design choice(s) for this class?

Suppose we have

class Foo
{
public:
    Foo(A a, B b);
    //other member functions and variables
}

class Bar
{
public:
    Bar();
private:
    Foo foo;
    //other member variables
}

Ideally the member foo of class Bar should be initialized using the initialization lists. However if the initialization of foo depends on some operations in Bar's constructor, initialization lists won't be an option. And since foo must be initialized before entering Bar's constructor, what is(are) the best design choice(s) here?

I searched around and found a few options like having a std::unique_ptr<Foo> pfoo; (Best practice for deferred initialization of private class members), but somehow the design seemed sloppy to me.

I want to avoid using raw pointers Foo * as possible.

Thanks in advance for your input!

Aucun commentaire:

Enregistrer un commentaire