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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:05:25+00:00 2026-06-17T23:05:25+00:00

I want to have a vector of pointers to objects in my class. To

  • 0

I want to have a vector of pointers to objects in my class. To avoid making a destructor for it I wanted to use std::unique_ptr, as the objects are created/owned/destructed in my class, but I have a compiler error I can’t understand. Next code will serve as a short sample for my problem:

std::unique_ptr<int> createPtr(int value)
{
    std::unique_ptr<int> ptr(new int(value));
    return ptr;
};

int main()
{
    std::vector<std::unique_ptr<int>> vec;
    vec.push_back(createPtr(1));

    std::unique_ptr<int> ptr = createPtr(2);
    vec.push_back(ptr);//error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
}

Can you please explain me why I get this error and what is the correct usage for std::unique_ptr?

  • 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-17T23:05:26+00:00Added an answer on June 17, 2026 at 11:05 pm

    Consider the vec.push_back(). It has two overloads which take either const std::unique_ptr<int>& or std::unique_ptr<int>&&. The first overload can never be used. This is because the vector<> requires the type to be assignable, or movable (C++11 addition). ‘Assignability’ implies copying. push_back(const T&) will try to copy (assign) the incoming value to the new space at the end of the container. std::unique_ptr<> represents a resource that is owned by a single owner. By copying it (the pointer) multiple owners would be present. Because of that unique_ptr is not copyable.

    Having said all of it, you can only use the T&& overload.

    createPtr() returns std::unique_ptr<int>, but as this is a temporary result (function return value) it is considered a rvalue reference (implicitly). That is why this can be used.

    ptr is just a std::unique_ptr<int> which is a lvalue reference (no matter if you put && next to it, as named rvalues are still treated as lvalues). Lvalue is never implicitly converted to rvalue (completely unsafe). But you can basically tell the compiler “Ok, you can take the object I pass you, and I promise I won’t expect the argument to be left intact” by using std::move().

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

Sidebar

Related Questions

I have some vectors of class A objects: std::vector<A> *V1; std::vector<A> *V2; etc there
I want to have a deep copy of an vector with pointers to objects,
I have a vector with shared pointers: std::vector<std::shared_ptr<DescriptorsNs::Descriptor> > mDescriptorList; Now i want to
I have a class that adapts std::vector to model a container of domain-specific objects.
I have a vector that I want to use to create a heap. I'm
I have a vector declared as: vector<Curve> workingSet; Curve is a class I've created
I have a vector with raw pointers (no, I cannot use smart pointers) and
I have a class that has a vector of another class objects as a
I have a class AudioDeviceManager that passes a std::vector by reference to an AudioAnalyzer
I have this code to represent bank: class Bank { friend class InvestmentMethod; std::vector<BaseBankAccount*>

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.