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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:51:59+00:00 2026-05-18T19:51:59+00:00

Back on my crazy AutoArray thingy … (quoting important bits from there: class AutoArray

  • 0

Back on my crazy AutoArray thingy… (quoting important bits from there:

class AutoArray
{
    void * buffer;
public:
    //Creates a new empty AutoArray
    AutoArray();
    //std::auto_ptr copy semantics
    AutoArray(AutoArray&); //Note it can't be const because the "other" reference
                           //is null'd on copy...
    AutoArray& operator=(AutoArray);
    ~AutoArray();
    //Nothrow swap
    // Note: At the moment this method is not thread safe.
    void Swap(AutoArray&);
};

)

Anyway, trying to implement the copy constructor. There’s a piece of client code (not yet committed into bitbucket because it won’t build) that looks like this:

AutoArray NtQuerySystemInformation(...) { ... };

AutoArray systemInfoBuffer = NtQuerySystemInformation(...);

This fails because the copy constructor takes a non-const reference as an argument …. but I don’t see how you could modify the copy constructor to take a const reference, given that the source AutoArray used in the assignment is modified (and therefore wouldn’t be const). You can’t modify things to use pass by value of course, because it’s the copy constructor and that’d be an infinite loop!

If I was using auto_ptr, this would be valid:

std::auto_ptr NtQuerySystemInformation(...) { ... };

std::auto_ptr systemInfoBuffer = NtQuerySystemInformation(...);

How then, can a class with auto_ptr‘s copy semantics be possible?

  • 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-05-18T19:52:00+00:00Added an answer on May 18, 2026 at 7:52 pm

    auto_ptr uses a dirty trick.

    I’ll use a dumbed-down class named auto_int to demonstrate just the copy construction functionality without bringing in any complexities introduced by templates or inheritance. I think the code is mostly correct, but it’s untested. Our basic auto_int look something like this:

    class auto_int
    {
    public:
    
        auto_int(int* p = 0) : p_(p) { }
    
        ~auto_int() { delete p_; }
    
        // copy constructor taking a non-const reference:
        auto_int(auto_int& other) 
            : p_(other.release()) { }
    
        int* release() 
        {
            int* temp = p_;
            p_ = 0;
            return temp;
        }
    
    private:
    
        int* p_;
    };
    

    With this basic auto_int, we can’t copy a temporary object. Our goal is to be able to write something like:

    auto_int p(auto_int(new int()));
    

    What we can do is use a helper class. For auto_ptr, this is called auto_ptr_ref. We’ll call ours auto_int_ref:

    class auto_int;
    
    class auto_int_ref 
    {
    public:
        auto_int_ref(auto_int* p) : p_(p) { }
    
        auto_int& ref() { return *p_; }
    
    private:
        auto_int* p_;
    };
    

    Basically, an instance of this class just stores a pointer to an auto_int and allows us to use it as a “reference” to an auto_int.

    Then in our auto_int class we need two additional functions. We need another constructor that takes an auto_int_ref and we need a conversion operator that allows an auto_int to be implicitly converted to an auto_int_ref:

    auto_int(auto_int_ref other)
        : p_(other.ref().release()) { }
    
    operator auto_int_ref() { return this; }
    

    This will allow us to “copy” a temporary while still having the copy constructor take a non-const reference. If we look again at our sample code:

    auto_int p(auto_int(new int()));
    

    What happens is we construct a new temporary auto_int and pass new int() to the constructor that takes an int*. This temporary is then converted to an auto_int_ref that points to it, using the operator auto_int_ref(), and the auto_int constructor that takes an auto_int_ref is used to initialize p.

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

Sidebar

Related Questions

Back in the ITAR era, there was a popular sig that performed Diffie-Hellman key
Back in the old days, Help was not trivial but possible: generate some funky
Back in VB6, I wrote a few functions that would let me code without
Back in the days of Unix, you couldn't even close a software without reading
Back in the 90s when I first started out with MFC I used to
Back in 2000 (when .NET was unleashed upon us IIRC) it was an innovative
Back in college, only the use of pseudo code was evangelized more than OOP
back story: I am designing a portfolio website for myself. on its home page,
Back in the scripted ASP and ColdFusion days, I used to work on projects
Going back to my previous question on OCSP, does anybody know of reliable OCSP

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.