+ Reply to Thread
Results 1 to 2 of 2

Thread: unique_ptr fails program if used as parameter in a function. why?

  1. #1

    unique_ptr fails program if used as parameter in a function. why?

    I have following code snippet:

    Code:
    void UseUniquePtr(std::unique_ptr<CMyObject> obj)
    {
    	//obj
    }
    
    void CDlg::OnBnClickedOk()
    {
    	std::unique_ptr<CMyObject> p(new CMyObject) ;
    	UseUniquePtr(p) ;
    }
    Why compiler fails with "cannot access private member declared in class" error? Any idea would be greatly appreciated!

  2. #2
    You cannot assign ownership using unique_ptr. This is what unique_ptr are for.

    You can have below solution:

    Code:
    void UseUniquePtr(std::unique_ptr<CMyObject> obj)
    {
    	//obj
    }
    
    void CDlg::OnBnClickedOk()
    {
    	std::unique_ptr<CMyObject> p(new CMyObject) ;
    	UseUniquePtr(std::move(p)) ;
    }
    I thought, it may help you.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts