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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:34:47+00:00 2026-05-20T05:34:47+00:00

Possible Duplicate: FAQ : How to pass objects to functions in C++? Pointer vs.

  • 0

Possible Duplicate:
FAQ: How to pass objects to functions in C++?
Pointer vs. Reference

Hi all,
in c/c++,
we can pass a object as call by reference or passing pointer of the object.
for example:
i want to create a function which will take string vector as input and output a map that contains some value for each string. the return value of the function is bool, which indicate success or failure.

function (call by reference)

bool calculateSomeValue( vector<string> input, map<string, double>& result)
{
//// bla bla bla
return true/false;
}

function (using pointer )

bool calculateSomeValue( vector<string> input, map<string, double>* result)
{
//// bla bla bla
return true/false;
}

which one is best? does any one have any idea of the pros and cons of these two options?

thanks in advance.

  • 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-20T05:34:48+00:00Added an answer on May 20, 2026 at 5:34 am

    This is a matter of style. At Google (see Google C++ style guidelines), the following would be preferred:

    bool CalculateSomeValue(
        const vector<string>& input, map<string, double>* result);
    

    This is because using a pointer requires the explicit use of an ampersand at the call site:

     CalculateSomeValue(input, &result);
    

    As opposed to the way it would be invoked with a reference type:

     CalculateSomeValue(input, result);
    

    By forcing the use of an ampersand in cases where parameters are modified, it is clear at the call site what will happen. When using references, it becomes necessary to lookup the documentation for each and every function to know whether it has the potential to modify its input parameters.

    However, the use of a pointer has its downsides, too. In particular, the use of a pointer means that you shift responsibility of handling null from the caller where the pointer would be dereferenced (if the variable is a pointer and not merely an address-of expression with a local variable) to the function. That is, when using a pointer type, CalculateSomeValue needs to check for nullptr (or needs to clearly document that it requires this checking in the caller), whereas the use of a reference type is self-documenting and clearly indicates that a non-null reference is expected.

    For this particular situtation, I personally highly recommend a hybrid approach:

    bool CalculateSomeValue(
       const std::vector<std::string>& input,
       Output<map<string, double>> result);
    

    … where Output<T> is created by a function with the signature:

     template<typename T> Output<T> WriteTo(T& output_ref);
    

    … and where Output<T> overloads operators ->, *, etc. This basically forces the call sites to be explicit in the fact that the input will be mutated by requiring:

     CalculateSomeValue(input, WriteTo(result));
    

    … as opposed to:

     CalculateSomeValue(input, result);
    

    … while simultaneously gaining the non-null semantics/syntax of references.

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

Sidebar

Related Questions

Possible Duplicate: How can I downgrade the version of an SVN working copy? http://subversion.tigris.org/faq.html#working-copy-format-change
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: How do you give a C# Auto-Property a default value? Hi all:
Possible Duplicate: FAQ : Undefined Behavior and Sequence Points Different outputs on different compiler?
Possible Duplicate: Lock the Android device programmatically I want to lock the screen through
Possible Duplicate: Can we change the device time using an application? is that possible
Possible Duplicate: How to store an IP in mySQL I want to get the
Possible Duplicate: How can I increment a date by one day in Java? I
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the

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.