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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:40:52+00:00 2026-06-10T12:40:52+00:00

I have a vector: std::vector<std::unique_ptr<myClass>> vec; //filling the vector vec.push_back(std::unique_ptr<myClass>(new myClass())); vec.push_back(std::unique_ptr<myClass>(new myClass())); vec.push_back(std::unique_ptr<myClass>(new

  • 0

I have a vector:

std::vector<std::unique_ptr<myClass>> vec;
//filling the vector
vec.push_back(std::unique_ptr<myClass>(new myClass()));
vec.push_back(std::unique_ptr<myClass>(new myClass()));
vec.push_back(std::unique_ptr<myClass>(new myClass()));

I need a handle to a specific instance of myClass that doesn’t need to worry about ownership, e.g:

std::unique_ptr<myClass>& myInstance;

The problem is that myInstance will need point to different instances of myClass during runtime.

I know I could use an std::shared_ptr, but I want to keep them std::unique_ptr‘s so if something already initialized is added the ownership is transferred to vec.

How should I retain a pointer to an std::unique_ptr?

Additionally, would it considered bad practice to use a raw pointer here? The pointer won’t need to manage any resources, but I don’t want my code to get confusing.

  • 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-10T12:40:53+00:00Added an answer on June 10, 2026 at 12:40 pm

    The comments to your post indicate that using a raw pointer in a “non-owning” capacity should be OK. I tend to concur with that general sentiment, but your case needs further examination.

    For your situation you should not use “a pointer to an std::unique_ptr”. The problem is that when you (or a maintenance programmer) adds to the vector, the vector could reallocate. If it does, your pointer is invalidated and must not be used.

    One option would be to use the index into the vector instead of a pointer. This has the problem of breaking if you ever delete or rearrange entries in the vector.

    Another, more robust, option is to use a pointer direct to the actual myClass object rather than the unique_ptr that owns it. This option is enabled by your use of a vector of unique_ptrs of objects instead of the more usual vector of objects.

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

Sidebar

Related Questions

I have a std::vector<uint8_t> that contains strings at specific offsets. Here's a shortened dump:
So I have a std::vector<std::unique_ptr<Base>> vec and I'm trying to sort it dynamically, given
I have an std::vector of handle objects. I have to wait on these handle
I have an std::vector that holds all the objects and is passed to them
I have a std::vector. I want to create iterators representing a slice of that
I have a std::vector with the element as pair and need to find all
Say, I have a std::vector<SomeClass *> v; in my code and I need to
I have a vector object: std::vector<std::vector<MyClass>> _matrix; It is 2d array with some data.
Problem I have a template container MyContainer<std::unique_ptr<Foo>> which has a std::deque<T> and a std::vector<T>
I can think of three ways of filling a std::vector Suppose we have vector<int>

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.