Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8229145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:42:06+00:00 2026-06-07T16:42:06+00:00

I am trying to learn how to use smart pointers and understand ownership. When

  • 0

I am trying to learn how to use smart pointers and understand ownership. When I pass an auto_ptr to a function by value, the function takes exclusive ownership of that pointer. So when the function finishes up, it deletes the pointer that I passed to it.

However, I get a compile error when I try doing this with a unique_ptr, as if copy assignment is disabled for unique_ptrs. Passing a unique_ptr by reference does not seem to transfer ownership, it merely gives the function a reference to the unique_ptr.

How do I get auto_ptr‘s behavior with passing ownership to function to work with unique_ptrs? I would appreciate a link to a detailed tutorial on unique_ptr, as so far the ones I’ve read seem to only talk about auto_ptr or talk about the smart pointers available with Boost and seem to ignore unique_ptr because shared_ptr covers it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:42:07+00:00Added an answer on June 7, 2026 at 4:42 pm

    However, I get a compile error when I try doing this with a
    unique_ptr, as if copy assignment is disabled for unique_ptrs.

    It is. unique_ptr has one, and only one, owner. It cannot be copied because that would result in two owners. In order to pass it by value into another function, the original owner must relinquish ownership, using std::move.

    In order to use unique_ptr, you must understand move semantics.

    auto_ptr is simply a hacky approximation to true move semantics that doesn’t actually work. It’s best to simply forget this class ever existed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying learn the use of lambda expressions and hence still struggling to
I am trying to learn and use an SDK for a vendor's product. Unfortunately,
I am trying to learn to use buffer overflow attack in Ubuntu. Unfortunately, I
I am trying to learn to use the wx packages to make GUI programs
I am relatively new to programming, and I am trying to learn to use
I'm trying to learn how to use AJAX in my rails apps so i've
I'm trying to learn how to use the force.layout features in D3.js and have
I am trying to learn how to use lists in C#. There are a
I'm trying to learn how to use oop in php. I'm also fairly new
I'm trying to learn how to use lift. I can create project skeleton by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.