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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:24:27+00:00 2026-06-04T12:24:27+00:00

I’m new to c++ coming from c#. The following code is not working and

  • 0

I’m new to c++ coming from c#. The following code is not working and I’m not sure what it needs. Any insight to help me understand why it isn’t working and what the appropriate changes it should have would be appreciated.

// GamePlayScreen derives from ScreenBase
std::unique_ptr<GameplayScreen> m_game(new GameplayScreen());

// MenuScreen derives from ScreenBase
std::unique_ptr<MenuScreen> m_menu(new MenuScreen());

// PauseScreen derives from ScreenBase
std::unique_ptr<PauseScreen> m_pause(new PauseScreen());        

std::vector<std::unique_ptr<ScreenBase>*> screens;

screens.push_back(&m_game); // this gets the error

I get compile error:

C2284 “cannot convert parameter 1 from ‘std::unique_ptr<_Ty> *’ to ‘std::unique_ptr<_Ty> *&&’

If I comment out the last line, it compiles fine.

Basically, I would like to have a collection of the derived items (or rather pointers to them). I’ve tried various ways of ‘pointing and/or referencing’ the parameter and various ways of establishing the T in vector<T> but the solution eludes me.

  • 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-04T12:24:29+00:00Added an answer on June 4, 2026 at 12:24 pm

    I’m reading a lot of:

    …you probably want shared_ptrs…

    I would encourage people to default to unique_ptr when unsure. Plopping down shared_ptr without memory ownership design is exactly what leads to cyclic memory leaks.

    Yes, you can create cyclic memory leaks with unique_ptr too. However my experience has been that when unique_ptr is used it encourages the designer to understand who owns what as the design evolves, making cyclic memory leaks less likely, and easier to debug when they do occur.

    And if during this design process, the designer discovers that shared ownership semantics is actually needed, then by all means, reach for shared_ptr (and probably weak_ptr too to break those cycles).

    Finally, bind your raw pointers to your smart pointers as quickly as possible. The following is a compilable sketch of the OP’s problem which follows best practices:

    #include <type_traits>
    #include <utility>
    #include <memory>
    #include <vector>
    
    // A general purpose factory function for unique_ptrs
    // Feel free to make this factory function more specific to your domain
    template <class T, class ...Args>
    typename std::enable_if
    <
        !std::is_array<T>::value,
        std::unique_ptr<T>
    >::type
    make_ScreenParts(Args&& ...args)
    {
        return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
    }
    
    // Your class hierarchy
    struct ScreenBase
    {
        // Don't forget to make your destructors virtual
        virtual ~ScreenBase() = default;
    };
    
    // Future-proof your code with typedef's
    // If / when you need to switch smart pointer types
    //  or container types, you'll thank yourself
    typedef std::unique_ptr<ScreenBase> ScreenPtr;
    typedef std::vector<ScreenPtr> ScreenContainer;
    
    struct GameplayScreen
        : public ScreenBase
    {
    };
    
    struct MenuScreen
        : public ScreenBase
    {
    };
    
    struct PauseScreen
        : public ScreenBase
    {
    };
    
    int main()
    {
        // raw pointers never exposed here...
        ScreenContainer screens;
        screens.push_back(make_ScreenParts<GameplayScreen>());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.