LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2018, 11:08 PM   #1
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
passing shared pointer in C++


When writing a function to accept a shared pointer or unique pointer in C++, should I make the pointer pass by value or pass by reference?

Code:
auto sp = std::make_shared<Object>();
target_func( sp );
Code:
void target_func( std::shared_ptr<Object>& p ); // or
void target_func( std::shared_ptr<Object> p ); // which one is the norm?
Thanks
 
Old 10-09-2018, 01:59 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
As far as I know: there is no general norm for this. You can do this way or that way, it depends on you.
In general both can have pros and cons.
 
Old 10-09-2018, 04:11 AM   #3
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Passing by reference avoids reference counter increment so it may be faster (especially if you do it a lot in a highly multithreaded application) though the code may be considered a bit more confusing. Especially since, if you pass by reference, the method can change the shared pointer which could be a surprising behaviour; in this case you probably would want to a const reference. Then again, passing by reference stacks pointers on top of pointers so dereference could potentially be slower. Performance is hard to reason about in abstract.

As an aside, if you want to give away ownership of the object and pass it to the method, std::move the value and use &&.

Last edited by mina86; 10-09-2018 at 04:13 AM.
 
1 members found this post helpful.
Old 10-09-2018, 07:44 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
https://stackoverflow.com/a/8741626/240515
https://herbsutter.com/2013/06/05/go...er-parameters/

Last edited by dugan; 10-09-2018 at 07:47 PM.
 
Old 10-10-2018, 08:13 PM   #5
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
Thank you all.

I personally prefer passing by reference over by value, because the shared_ptr is not a simple type after all.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Passing a constant as a pointer to char. leadfree Programming 13 08-04-2014 01:42 AM
[SOLVED] Passing numerical constant to a function as pointer to int. leadfree Programming 12 08-03-2014 11:38 PM
passing pointer arguments correctly atlantis43 Programming 3 05-18-2014 06:30 AM
[SOLVED] c++ passing pointer variable as a reference parameter chinho Programming 4 01-26-2011 08:19 PM
passing function pointer as argument worldmagic Programming 7 08-04-2004 03:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration