lundi 27 décembre 2021

How to make a unique_ptr in a class a friend of another class

class Test;

class Base {

};

class Child : public Base {
    friend class Test;  // <--- not working?
protected:
    Child() {}
};

class Test {
public:
    Test() : t{std::make_unique<Child>()} {}
private:
    std::unique_ptr<Base> t;
};

There is a base class and a child class as shown, and another class containing a std::unique_ptr<Base> t, which needs to be initialized with a child object.

However, it's not working, I always get the error: error: ‘Child::Child()’ is protected.

It doesn't seem that friend class Test works as expected.

BTW, I'm working with C++14.

Aucun commentaire:

Enregistrer un commentaire