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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:09:33+00:00 2026-05-28T02:09:33+00:00

How do I allow a class with a copy constructor that takes a non-const

  • 0

How do I allow a class with a copy constructor that takes a non-const reference to be copy-constructed from temporaries?

The background is this:

I have a function that should return a list of pointers to objects that all inherit from Base, so I need something like vector<Base*>.
Given that vector<auto_ptr> is not much of an option, I wanted to write a simple wrapper around vector<Base*> that deletes all elements in its destructor.

I’m facing the following problem:

My class has a copy constructor as follows:

auto_list(auto_list& rhs);

so that I can copy the pointer list to the new instance and clear it in the old one.

But obviously, that won’t work with return values because temporaries don’t bind to a non-const reference.
Seeing that auto_ptr can be returned from functions, how did they implement it?

Note: I can’t use C++11 or boost, so move semantics or unique_ptr are not an option.

If it helps, this is my code so far:

template <typename T> class auto_list
{
private:

    vector<T*> pointers;

public:

    auto_list(vector<T*>& pointers)
    {
        this->pointers = pointers;
    }

    auto_list(auto_list& rhs)
    {
        this->pointers = rhs.pointers;
        rhs.pointers.clear();
    }

    ~auto_list()
    {
        for(typename vector<T*>::const_iterator it = this->pointers.begin(); it != this->pointers.end(); it++)
        {
            delete (*it);
        }
    }

    auto_list& operator=(auto_list& rhs)
    {
        this->pointers = rhs.pointers;
        rhs.pointers.clear();
    }

    vector<T*> get_pointers() const
    {
        return this->pointers;
    }
};
  • 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-28T02:09:34+00:00Added an answer on May 28, 2026 at 2:09 am

    This class will be rather confusing to use, just as auto_ptr itself, and I urge you to use more sensible smart pointers. Even if you do have a good reason for not using Boost (and for the life of me I can’t think why not), how about std::tr1::shared_ptr?

    If you’re determined to pursue this course, then auto_ptr solves the problem of initialising from a temporary by wrapping a reference in a class (auto_ptr_ref). The wrapper is created by a conversion function from *this (which is an lvalue, and therefore can be bound to a non-const reference), and can then be passed by value to a constructor.

    You can do something similar:

    template <typename T> class auto_list_ref
    {
        friend class auto_list<T>;
        auto_list_ref(auto_list<T> & ref) : ref(ref) {}
        auto_list<T> & ref;
    };
    
    template <typename T> class auto_list
    {
    public:
        // Add a constructor and conversion operator as follows:
    
        auto_list(auto_list_ref<T> rhs)
        {
            this->pointers = rhs.ref.pointers;
            rhs.ref.pointers.clear();
        }
    
        operator auto_list_ref<T>() {return auto_list_ref<T>(*this);}
    };
    

    Here is a demonstration.

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

Sidebar

Related Questions

I have a generic class that should allow any type, primitive or otherwise. The
I want to have a class property that allow for an expression to take
How can I allow a non-const reference to convert to a const reference in
I have a class that takes many arguments to create. It’s a sort of
I have a class on which I want to allow several (~20+) configuration options.
Is there a PHP class/library that would allow me to query an XHTML document
Is there any html form class(text box) that does not allow null values. The
I'd like to implement a simple class (in Java) that would allow me to
How should I write a copy constructor for my singleton class to prevent the
Consider this simple class: template<class T> class Foo{ public: Foo(T const& val) : _val(val)

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.