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

前端 未结 4 1870
清歌不尽
清歌不尽 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:42

    In your case the function make_unique is trying to create an instance of Spam and that function is not a friend. Calling a non-friend function from inside a friend function does not imbue the non-friend function with friend status.

    To solve this you can write in Foo:

    std::unique_ptr spam(new Spam(10));
    

提交回复
热议问题