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

The Archive Base Latest Questions

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

When passing objects into functions, do the same rules apply to smart pointers as

  • 0

When passing objects into functions, do the same rules apply to smart pointers as to other objects that contain dynamic memory?

When I pass, for example, a std::vector<std::string> into a function I always consider the following options:

  1. I’m going to change the state of the vector object, but I do not want those changes reflected after the function has finished, AKA make a copy.

    void function(std::vector<std::string> vec);
    
  2. I’m going to change the state of the vector object, and I do want those changes reflected after the function has finished, AKA make a reference.

    void function(std::vector<std::string> & vec);
    
  3. This object is pretty big, so I’d better pass a reference, but tell the compiler not to let me change it.

    void function(std::vector<std::string> const& vec);  
    

Now is this the same logic with smart pointers? And when should I consider move semantics? Some guidelines on how I should pass smart pointers is what I desire most.

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

    Smart pointers have pointer semantics, not value semantics (well, not the way you mean it). Think of shared_ptr<T> as a T*; treat it as such (well, except for the reference counting and automatic deletion). Copying a smart pointer does not copy the object it points to, just like copying a T* does not copy the T it points to.

    You can’t copy a unique_ptr at all. The whole point of the class is that it cannot be copied; if it could, then it wouldn’t be a unique (ie: singular) pointer to an object. You have to either pass it by some form of reference or by moving it.

    Smart pointers are all about ownership of what they point to. Who owns this memory and who will be responsible for deleting it. unique_ptr represents unique ownership: exactly one piece of code owns this memory. You can transfer ownership (via move), but in so doing, you lose ownership of the memory. shared_ptr represents shared ownership.

    In all cases, the use of a smart pointer in a parameter list represents transferring ownership. Therefore, if a function takes a smart pointer, then it is going to claim ownership of that object. If a function isn’t supposed to take ownership, then it shouldn’t be taking a smart pointer at all; use a reference (T&) or if you have need of nullability, a pointer but never store it.

    If you are passing someone a unique_ptr, you are giving them ownership. Which means, by the nature of unique ownership, you are losing ownership of the memory. Thus, there’s almost no reason to ever pass a unique_ptr by anything except by value.

    Similarly, if you want to share ownership of some object, you pass in a shared_ptr. Whether you do it by reference or by value is up to you. Since you’re sharing ownership, it’s going to make a copy anyway (presumably), so you might as well take it by value. The function can use std::move to move it into class members or the like.

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

Sidebar

Related Questions

I know that when it comes to passing objects in java, the reference of
Im using a BaseAdapter and passing to it a List of objects that have
I've been struggling with passing Java objects from Java through JSNI (gwt-exporter generated) into
I'm working with legacy Java code which returns java.lang.object. I'm passing it into a
I know when we are passing objects we are passing its reference as a
Having some trouble with R's garbage collection, when passing objects to C++. We have
I use std::tr1::shared_ptr extensively throughout my application. This includes passing objects in as function
After reading about the problem of passing empty std::string objects between DLLs and EXEs,
I have an ArrayCollection of objects. I'm passing this array to a horizontallist as
I have two questions related to function objects and function pointers, Question : 1

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.