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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:29:09+00:00 2026-05-26T13:29:09+00:00

I had been doing dependency injection using raw pointers and I decided to convert

  • 0

I had been doing dependency injection using raw pointers and I decided to convert my code to use shared_ptr. This works but I’m wondering if I could use unique_ptr instead? In my example below, MyClass would manage the lifetime of the credit card service.

class PaymentProcessor
{
    PaymentProcessor(?? creditCardService):
      :creditCardService_(creditCardService)
      {

      }

private:
   CreditCardService *creditCardService_;     
}

class MyClass
{ 
public:
   void DoIt()
   {
     creditCardService_.reset(new VisaCardService());
     PaymentProcessor pp(creditCardService_);
     pp.ProcessPayment();
   }

private:   
   std::unique_ptr<CreditCardService> creditCardService_;
}

Can you pass a unique_ptr to another class where the other class is just “using” the pointer (without owning it??)? If so is this a good idea and what should the type of the parameter be in the constructor for PaymentProcessor?

UPDATE

In the example as shown above I can alternatively create a VisaCardService variable on the stack and have the PaymentProcessor constructor take this as a reference parameter. This seems to be the recommended C++ practice. However, in the case where the concrete type of creditCardService_ is not known until runtime (e.g., the user chooses the particular Credit Card Service to use at runtime), is using std::unique_ptr with references the best solution?

  • 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-05-26T13:29:09+00:00Added an answer on May 26, 2026 at 1:29 pm

    Can you pass a unique_ptr to another class where the other class is
    just “using” the pointer (without owning it??)?

    In that case, change the pointer to reference :

    class PaymentProcessor
    {
    public:
        PaymentProcessor(CreditCardService & creditCardService_):
          :creditCardService_(creditCardService_)
          {
          }
    
    private:
       CreditCardService &creditCardService_;     
    };
    
       void DoIt()
       {
         creditCardService_.reset(new VisaCardService());
         PaymentProcessor pp(*creditCardService_);
         pp.ProcessPayment();
       }
    

    If you still want to use a pointer, then you need to use get method :

    class PaymentProcessor
    {
    public:
        PaymentProcessor(CreditCardService * creditCardService_):
          :creditCardService_(creditCardService_)
          {
          }
    private:
       CreditCardService *creditCardService_;     
    };
    
       void DoIt()
       {
         creditCardService_.reset(new VisaCardService());
         PaymentProcessor pp(creditCardService_.get());
         pp.ProcessPayment();
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a web freelancer and had been doing all coding by myself using
The ability to use BitKeeper free of charge had been withdrawn by the copyright
I have been doing some functional programming and had a question. Perhaps I might
I have recently been doing some circle detection in OpenCV and I have had
I have a (custom) embedded jetty launcher which I had been using to develop/test
Hopefully this is a quickie: I've been doing a lot of Java/Google Web Toolkit
I have been doing socket programming for many years, but I have never had
I had been doing some file IO in a project i am currently working
Been doing some brief research into multithreaded C# event handling because I had a
I've been doing server-side development for several years, and have not had much (read

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.