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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:44:23+00:00 2026-06-11T16:44:23+00:00

I have a list of smart pointers where each pointer points to a separate

  • 0

I have a list of smart pointers where each pointer points to a separate Entity class.

std::list<std::unique_ptr<Entity>> m_entities;

I would like the constructor to handle the assigning of each pointer to a std::list class as it is “automatically” handled by the code on class instantiation. However, if this design is bad then I would welcome a better alternative as it only makes sense to me coming from a C# background.

Entity::Entity(Game &game) 
    : m_game(game),             
    m_id(m_game.g_idGenerator->generateNewID())             
{
    m_game.m_entities.push_back(std::unique_ptr<Entity>(this));
}

The main problem I have encountered with this method is that the Entity class’ lifetime is unmanaged by the Entity class.

For example if I allocate an Entity class on the stack it will call the Entity destructor after leaving the method in which it was allocated and the pointer will no longer be valid.

I therefore considered the alternative of creating a smart pointer, allocating the Entity class to the heap and then explicitly adding the pointer to the list.

std::unique_ptr<Entity> b(new Entity(*this));
m_entities.push_back(b);   // ERROR

This produces the following error

error C2664: 'void std::list<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'std::unique_ptr<_Ty>' to 'std::unique_ptr<_Ty> &&'

What would be considered the best approach for allocating each pointer to the list and is a constructor based version possible?

I’m currently thinking that it is the list of smart pointers that should handle the lifetime for each Entity class and that assigning pointers in a constructor is not a good design choice. In that case I should probably create a CreateEntity method that adds the pointer to list rather than let the constructor handle it. Is this better?

I considered what type of smart pointer would be appropriate for this operation after reading through questions found here, here and here (offsite). It is difficult to get an exact answer based on what I’ve read so far though as they all offer somewhat conflicting advice.

  • 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-11T16:44:24+00:00Added an answer on June 11, 2026 at 4:44 pm

    Using constructor this way is definitely not good idea because constructor has no information about how object is created and controlled – on the stack, statically, dynamically by some smart pointer, dynamically by dumb pointer?

    To solve this problem you could use static factory method to create Entity instances:

    class Entity
    {
    public:
    
       // Variant with unique ownership
       static void CreateGameEntity(Game& game)
       {
         std::unique_ptr<Entity> p(new Entity());
         game.m_entities.push_back(std::move(p));
       }
    
       // OR (you cannot use both)
    
       // Variant with shared ownership
       static std::shared_ptr<Entity> CreateGameEntity(Game& game)
       {
         std::shared_ptr<Entity> p(new Entity());
         game.m_entities.push_back(p);
         return p;
       }
    
    private:
    
       // Declare ctors private to avoid possibility to create Entity instances
       // without CreateGameEntity() method, e.g. on stack.
       Entity();
       Entity(const Entity&);
    };    
    

    Which smart pointer to use? Well, this depends on your design. If Game object solely owns Entity instances and completely manages their lifetime, using std::unique_ptr is OK. If you need some kind of shared ownership (e.g. you have several Game objects that can share same Entity objects) you shall use std::shared_ptr.

    Also in case of unique ownership you may use Boost Pointer Container library. It contains specialized owning pointer containers like ptr_vector, ptr_list, ptr_map etc.

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

Sidebar

Related Questions

I have a list of smart pointers. I want some of these smart pointers
Would it be smart to have a vector in an object with a list
Let's say I have a: std::vector<std::list<Node> > And let's say Node is: class Node
I have list of input area in the form with id like contact1_title, contact2_title,
I have a function which takes a variable number of pointers, which I would
I have a vector with raw pointers (no, I cannot use smart pointers) and
I have a scenario where I'm pushing change-lists to another system. Each list contains
I have a list of items. Each of them is a square with a
I have a file that contains serialized Java classes. I would like to parse
When I have a class that contains pointers as member variables what type of

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.