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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:52:47+00:00 2026-05-27T06:52:47+00:00

I want to pass an object by smart-pointer reference to a function. The function

  • 0

I want to pass an object by smart-pointer reference to a function. The function may change the value of the referenced object, but may not change the reference itself. There are two obvious ways to handle this.

The first approach to pass the shared_ptr by value – it is the reference, so doesn’t itself need to be passed by reference. The apparent issue with this is copying of the reference, which suggests some reference-counting overhead.

void foo (shared_ptr<bar> p)

The second approach is to pass the shared_ptr by const reference – avoiding the copying of the shared_ptr instance, but instead implying that accesses to the referenced object need two layers of dereferencing instead of one.

void foo (const shared_ptr<bar> &p)

In practice, these theoretical overheads will normally be trivial and irrelevant. Which suggests to me that instead of choosing one approach or the other for each individual case, I should almost always follow some standard convention. Which leads to the question…

Is there a standard convention for which of these approaches I should normally choose? If so, which is the conventional choice?

EDIT – Probably worth mentioning – one reason to consider the pass-by-const-reference case is because there’s a pre-existing convention that most class/struct instances are passed by const-reference rather than by value, and shared_ptr is a class. Of course it isn’t a heavyweight class (the cost of copying is small), so the reasons behind that older convention may not apply.

  • 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-27T06:52:47+00:00Added an answer on May 27, 2026 at 6:52 am

    Always pass shared_ptrs by value. If you pass a reference, you might run into the problem that a call to a function of the object managed by the shared_ptr might just reset it, and now you’ve got a dangling pointer. If you pass by value, you ensure that the object will survive the current function call.

    See here for much, much more information.


    Example:

    #include <memory>
    #include <iostream>
    
    std::shared_ptr<int> ptr(new int(42));
    
    void foo(){
      ptr.reset();
    }
    
    void bar(std::shared_ptr<int> const& p){
      foo();
      std::cout << *p;
    }
    
    int main(){
      bar(ptr);
    }
    

    This should be taken with a pinch of salt. It can be adapted to prove that no type should ever be passed by const-reference – see for example http://ideone.com/1IYyC, which Benjamin Lindley pointed out in the comments.

    However, more complex variations of this kind of issue do arise by accident in practice. This is the reason, for example, why we are warned that iterators (as well as const-reference return values) are invalidated by methods that mutate the referenced container. These rules are easy enough to follow in general, but occasionally more indirect and unexpected examples can catch people by surprise.

    That being the case, it’s best to avoid the extra layer of referencing when it’s not needed.

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

Sidebar

Related Questions

All i want is to pass a HTML (DOM object) from javascript to Actionscript.
I want to create light object data-package to pass between client and server applications.
In short, I want to use an object literal to allow me to pass
I want to pass an array of objects from my QtScript to C++ but
I want to pass an integer value to a form in .Net so that
I want to pass an enum value as command parameter in WPF, using something
I want to pass in the tType of a class to a function, and
My requirement is when I pass some paramenter query need to send object. not
I know that stuff of calling the new activity and pass the object value
I want to pass an int list (List) as a declarative property to a

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.