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 8083219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:23:31+00:00 2026-06-05T17:23:31+00:00

I have been reading up on the c++ auto_ptr and unique_ptr and stuff and

  • 0

I have been reading up on the c++ auto_ptr and unique_ptr and stuff and thought to try and use them in a class I am playing with… but I was having trouble getting it to work…

How would I convert these pointers to auto pointers or some equivalent so the deletion of the pointers is handled automatically?

Header – http://ideone.com/Z9bc5
Body – http://ideone.com/WfwBY

At the moment it is working using normal pointers but I sometimes get a access violation error. I am pretty sure I know what it causing it.. but the “best” way might be to use the automatic deletion stuff recently added to c++11?

Thanks in advance.

  • 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-05T17:23:33+00:00Added an answer on June 5, 2026 at 5:23 pm

    Don’t use auto_ptr. Try one of unique_ptr or shared_ptr. Here’s Sutter explaining when to use which:

    When in doubt, prefer unique_ptr by default, and you can always later move-convert to shared_ptr if you need it. If you know from the start you need shared ownership, however, go directly to shared_ptr via make_shared (see #2 below).

    Also from his blog-post:

    3. What’s the deal with auto_ptr?

    auto_ptr is most charitably characterized as a valiant attempt to
    create a unique_ptr before C++ had move semantics.

    auto_ptr is now deprecated, and should not be used in new code. When
    you get a chance, try doing a global search-and-replace of auto_ptr to
    unique_ptr in your code base; the vast majority of uses will work the
    same, and it might expose (as a compile-time error) or fix (silently)
    a bug or two you didn’t know you had.

    So, your member declarations change from:

     sf::Texture * tSpriteSheet;
    

    to:

     std::unique_ptr<sf::Texture> tSpriteSheet;
    

    As for member functions which return a raw pointer you have but the obvious choice: You cannot return a unique_ptr if the class is not movable. So, you can either:

    • Keep the signature as-is
    • Return a const& unique_ptr<T>
    • Return a reference to the object

    Choose the one that suits your needs the best.

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

Sidebar

Related Questions

I have been reading the tutorial Controlling Access to Members of a Class .
I have been reading conflicting information about the use of #define macros in iOS
I have been reading the Phing documentation and it makes sense, but I'm not
I have been reading up on this, and it seems that if you use
I have been reading other questions and answers around this but I am not
Have been reading about async and tasks and been attempting to convert the CopyFileEx
I have been reading Rafactoring by Martin Fowler and in the beginning of the
I have been reading up on the changes that .NET4.5 will bring, and on
I have been reading about making ajax heavy applications more search engine friendly: https://developers.google.com/webmasters/ajax-crawling/docs/getting-started
I have been reading up on the technical basics of how SSL works (which

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.