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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:11:33+00:00 2026-05-27T13:11:33+00:00

I’ve a class Foo<T> which has a vector of smart pointers to Shape derived

  • 0

I’ve a class Foo<T> which has a vector of smart pointers to Shape derived classes.
I’m trying to implement an at(index) member function. Here’s what I would to do intuitively:

Foo<float> myfoo;
std::unique_ptr<Shape<float>> shape_ptr = myfoo.at(i);
shape_ptr->doSomething(param1, param2, ...);

When defining the at(index) function, I’m getting a compiler error message. Note that the move constructor was defined and that the Shape base class is abstract. Below, I’m giving some code for illustration purposes.

Furthermore, I found recently on the web an example on how to overload the assignment operator using std::move. I usually follow the Copy-Swap idiom. Which of those two ways for overloading the mentioned operator makes sense for my case? Below, I’m also illustrating the function’s definition.

template < typename T >
class Foo{

    public:

        Foo();
        Foo( Foo && );
        ~Foo();

        void swap(Foo<T> &);
        //Foo<T> & operator =( Foo<T> );
        Foo<T> & operator =( Foo<T> && );

        std::unique_ptr<Shape<T> > at ( int ) const; // error here!

        int size() const;

    private:

        std::vector< std::unique_ptr<Shape<T> > > m_Bank;
};

template < typename T >
Foo<T>::Foo( Foo && other)
    :m_Bank(std::move(other.m_Bank))
{

}

/*template < typename T >
void Filterbank<T>::swap(Filterbank<T> & refBank ){

    using std::swap;
    swap(m_Bank, refBank.m_Bank);
}

template < typename T >
Foo<T> & Filterbank<T>::operator =( Foo<T> bank ){

    bank.swap(*this);
    return (*this);
}*/

template < typename T >
Foo<T> & Foo<T>::operator =( Foo<T> && bank ){

    //bank.swap(*this);
    m_Bank = std::move(bank.m_Bank);
    return (*this);
}

template < typename T >
std::unique_ptr<Shape<T> > Foo<T>::at( int index ) const{
    return m_Bank[index]; // Error here! => error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
}
  • 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-27T13:11:34+00:00Added an answer on May 27, 2026 at 1:11 pm

    Q1: What to do with Foo::at( int ) const such that you can:

    myfoo.at(i)->doSomething(param1, param2, ...);
    

    without transferring ownership out of the vector<unique_ptr<Shape<T>>>.

    A1: Foo::at( int ) const should return a const std::unique_ptr<Shape<T> >&:

    template < typename T >
    const std::unique_ptr<Shape<T> >&
    Foo<T>::at( int index ) const
    {
        return m_Bank[index];
    }
    

    Now your can dereference the const unique_ptr and call any member of Shape they want (const or non-const). If they accidentally try to copy the unique_ptr, (which would transfer ownership out of Foo) they will get a compile time error.

    This solution is better than returning a non-const reference to unique_ptr as it catches accidental ownership transfers out of Foo. However if you want to allow ownership transfers out of Foo via at, then a non-const reference would be more appropriate.

    Q2: Furthermore, I found recently on the web an example on how to overload the assignment operator using std::move. I usually follow the Copy-Swap idiom. Which of those two ways for overloading the mentioned operator makes sense for my case?

    A2: I’m not sure what ~Foo() does. If it doesn’t do anything, you could remove it, and then (assuming fully conforming C++11) you would automatically get correct and optimal move constructor and move assignment operator (and the proper deleted copy semantics).

    If you can’t remove ~Foo() (because it does something important), or if your compiler does not yet implement automatic move generation, you can supply them explicitly, as you have done in your question.

    Your move constructor is spot on: Move construct the member.

    Your move assignment should be similar (and is what would be automatically generated if ~Foo() is implicit): Move assign the member:

    template < typename T >
    Foo<T> & Foo<T>::operator =( Foo<T> && bank )
    {
        m_Bank = std::move(bank.m_Bank);
        return (*this);
    }
    

    Your Foo design lends itself to being Swappable too, and that is always good to supply:

    friend void swap(Foo& x, Foo& y) {x.m_Bank.swap(y.m_Bank);}
    

    Without this explicit swap, your Foo is still Swappable using Foo‘s move constructor and move assignment. However this explicit swap is roughly twice as fast as the implicit one.

    The above advice is all aimed at getting the very highest performance out of Foo. You can use the Copy-Swap idiom in your move assignment if you want. It will be correct and slightly slower. Though if you do be careful that you don’t get infinite recursion with swap calling move assignment and move assignment calling swap! 🙂 Indeed, that gotcha is just another reason to cleanly (and optimally) separate swap and move assignment.

    Update

    Assuming Shape looks like this, here is one way to code the move constructor, move assignment, copy constructor and copy assignment operators for Foo, assuming Foo has a single data member:

    std::vector< std::unique_ptr< Shape > > m_Bank;
    

    …

    Foo::Foo(Foo&& other)
        : m_Bank(std::move(other.m_Bank))
    {
    }
    
    Foo::Foo(const Foo& other)
    {
        for (const auto& p: other.m_Bank)
            m_Bank.push_back(std::unique_ptr< Shape >(p ? p->clone() : nullptr));
    }
    
    Foo&
    Foo::operator=(Foo&& other)
    {
        m_Bank = std::move(other.m_Bank);
        return (*this);
    }
    
    Foo&
    Foo::operator=(const Foo& other)
    {
        if (this != &other)
        {
            m_Bank.clear();
            for (const auto& p: other.m_Bank)
                m_Bank.push_back(std::unique_ptr< Shape >(p ? p->clone() : nullptr));
        }
        return (*this);
    }
    

    If your compiler supports defaulted move members, the same thing could be achieved with:

        Foo(Foo&&) = default;
        Foo& operator=(Foo&&) = default;
    

    for the move constructor and move assignment operator.

    The above ensures that at all times each Shape is owned by only one smart pointer/vector/Foo. If you would rather that multiple Foos share ownership of Shapes, then you can have as your data member:

    std::vector< std::shared_ptr< Shape > > m_Bank;
    

    And you can default all of move constructor, move assignment, copy constructor and copy assignment.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from

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.