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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:29:50+00:00 2026-06-07T04:29:50+00:00

I am having my first attempt at using C++11 unique_ptr ; I am replacing

  • 0

I am having my first attempt at using C++11 unique_ptr; I am replacing a polymorphic raw pointer inside a project of mine, which is owned by one class, but passed around quite frequently.

I used to have functions like:

bool func(BaseClass* ptr, int other_arg) {
  bool val;
  // plain ordinary function that does something...
  return val;
}

But I soon realized that I wouldn’t be able to switch to:

bool func(std::unique_ptr<BaseClass> ptr, int other_arg);

Because the caller would have to handle the pointer ownership to the function, what I don’t want to. So, what is the best solution to my problem?

I though of passing the pointer as reference, like this:

bool func(const std::unique_ptr<BaseClass>& ptr, int other_arg);

But I feel very uncomfortable in doing so, firstly because it seems non instinctive to pass something already typed as _ptr as reference, what would be a reference of a reference. Secondly because the function signature gets even bigger. Thirdly, because in the generated code, it would be necessary two consecutive pointer indirections to reach my variable.

  • 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-07T04:29:52+00:00Added an answer on June 7, 2026 at 4:29 am

    If you want the function to use the pointee, pass a reference to it. There’s no reason to tie the function to work only with some kind of smart pointer:

    bool func(BaseClass& base, int other_arg);
    

    And at the call site use operator*:

    func(*some_unique_ptr, 42);
    

    Alternatively, if the base argument is allowed to be null, keep the signature as is, and use the get() member function:

    bool func(BaseClass* base, int other_arg);
    func(some_unique_ptr.get(), 42);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first attempt at using Selenium, and I am having trouble with
I'm having some problems upgrading Python on my Mac. For my first attempt, I
The problem I'm having is that the first URL works and the second one
The problem I'm having is that the first URL works and the second one
So this is my first time using cookies and i'm having some trouble setting
having my first attempt at rounded corners in a login form. Just doing the
I'm having my first go at using Google charts using a Ruby wrapper called
I'm a C# newbie who's having problems understanding why his first attempt at understanding
This is my first attempt at using Backbone.js, so I decided to make a
I'm having my first play around with Ember.js, and am falling at the first

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.