Friend function is unable to construct a unique pointer of the class

前端 未结 4 1873
清歌不尽
清歌不尽 2021-02-13 13:45

I have a certain design strategy where the constructor of my class is private and can only be constructed by friends of the class. Inside the friend function, I am trying to cre

4条回答
  •  我在风中等你
    2021-02-13 14:44

    In your example, Foo() is a friend, but it isn't the function that's creating the Spam - make_unique is internally calling new Spam itself. The simple fix is to just have Foo() actually construct the Spam directly:

    void Foo() {
        std::unique_ptr spam(new Spam(10));
    }
    

提交回复
热议问题