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

  • Home
  • SEARCH
  • 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 8900003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:59:48+00:00 2026-06-15T00:59:48+00:00

I was reading an article about effectively using auto_ptr . In there, the following

  • 0

I was reading an article about effectively using auto_ptr. In there, the following code was suggested as a correct piece of code:

// Example 10(c): Correct (finally!)
//
auto_ptr<String> f()
{
  auto_ptr<String> result = new String;
  *result = "some value";
  cout << "some output";
  return result;  // rely on transfer of ownership;
                  // this can't throw
}

But as far as I am aware, the assignment operator of auto_ptr only accepts another auto_ptr as the rhs — to avoid accidental misuse. So, is the following line a typo in the article, or is it really suppposed to work?

auto_ptr<String> result = new String;
  • 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-15T00:59:49+00:00Added an answer on June 15, 2026 at 12:59 am

    That line is indeed incorrect, but not for the reasons you think. It doesn’t call the assignment operator, but instead calls the constructor. When you say = when you first declare an object, it doesn’t create the object and then call the assignment operator. Rather, it just calls the constructor and passes that as an argument. So from this stand point, it’s “correct” because it’s using the constructor, not the assignment operator

    Or at least it would if it could. You’ll notice std::auto_ptr‘s constructor takes a pointer. However, it’s marked explicit, which means that the above “shortcut” isn’t allowed. You have to explicitly call the constructor (with parentheses), and cannot use = as a shortcut to do so. This is why it’s incorrect. If, instead, they said auto_ptr<String> result(new String);, all would be fine. Also, if the constructor wasn’t marked explicit, all would be fine. But as it is, it is not correct.

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

Sidebar

Related Questions

After reading an article about fonts, i tried the code and i am getting
I am reading an article in IEEE Computer magazine about using data mining on
I was reading this article about volatile fields in C#. using System; using System.Threading;
I'm reading and trying to apply this article about File Uploads , but there
I am reading an article about Entity Framework 4.0 that states the following: The
I am reading an article about Concurrency Runtime, and there is algorithm named work
After reading this article about elapsed time, I wrote a simple code to calculate
I was reading an article about how query expressions defer executions. Does that mean
I'm reading this documentation article about Custom Annotations in Doctrine . I understand that
Reading through this excellent article about safe construction techniques by Brain Goetz, I got

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.