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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:51:46+00:00 2026-05-12T09:51:46+00:00

Provided, I want to pass a modifiable parameter to a function, what should I

  • 0

Provided, I want to pass a modifiable parameter to a function, what should I choose: to pass it by pointer or to pass it by reference?

  1. bool GetFoo ( Foo& whereToPlaceResult );
  2. bool GetFoo ( Foo* whereToPlaceResult );

I am asking this because I always considered it the best practice to pass parameter by reference (1), but after examining some local code database, I came to a conclusion, that the most common way is (2). Moreover, the man himself (Bjarne Stroustrup) recommends using (2). What are the [dis]advantages of (1) and (2), or is it just a matter of personal taste?

  • 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-12T09:51:46+00:00Added an answer on May 12, 2026 at 9:51 am

    I prefer a reference instead of a pointer when:

    • It can’t be null
    • It can’t be changed (to point to something else)
    • It mustn’t be deleted (by whoever receives the pointer)

    Some people say though that the difference between a reference and a const reference is too subtle for many people, and is invisible in the code which calls the method (i.e., if you read the calling code which passes a parameter by reference, you can’t see whether it’s a const or a non-const reference), and that therefore you should make it a pointer (to make it explicit in the calling code that you’re giving away the address of your variable, and that therefore the value of your variable may be altered by the callee).

    I personally prefer a reference, for the following reason:

    1. I think that a routine should know what subroutine it’s calling
    2. A subroutine shouldn’t assume anything about what routine it’s being called from.

    [1.] implies that making the mutability visible to the caller doesn’t matter much, because the caller should already (by other means) understand what the subroutine does (including the fact that it will modify the parameter).

    [2.] implies that if it’s a pointer then the subroutine should handle the possibility of the parameter’s being a null pointer, which may be extra and IMO useless code.

    Furthermore, whenever I see a pointer I think, “who’s going to delete this, and when?”, so whenever/wherever ownership/lifetime/deletion isn’t an issue I prefer to use a reference.

    For what it’s worth I’m in the habit of writing const-correct code: so if I declare that a method has a non-const reference parameter, the fact that it’s non-const is significant. If people weren’t writing const-correct code then maybe it would be harder to tell whether a parameter will be modified in a subroutine, and the argument for another mechanism (e.g. a pointer instead of a reference) would be a bit stronger.

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

Sidebar

Ask A Question

Stats

  • Questions 168k
  • Answers 168k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, as seems to be the case with a lot… May 12, 2026 at 1:47 pm
  • Editorial Team
    Editorial Team added an answer yairchu has a good answer for your problem with printBody.… May 12, 2026 at 1:47 pm
  • Editorial Team
    Editorial Team added an answer You can look at using AspectJ for doing this, as… May 12, 2026 at 1:47 pm

Related Questions

I want to pass the string !changeme! to a java program on the command
I have a trigger in SQL Server, but I need to pass arguments to
I have a list I've built in Sharepoint, where one of the fields is
This should be fairly simple from what I can find online, but I can't

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.