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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:03:32+00:00 2026-05-24T06:03:32+00:00

I was coding up a C++ class today, and I wrote a function that

  • 0

I was coding up a C++ class today, and I wrote a function that took an argument as a reference rather than a pointer, something I rarely ever do. I’ve always passed by pointers. So I was about to change it back, and then I realized – I have no idea if I should, or if it even matters.

So I turn to you guys. I have three ways of passing parameters about:

//1: By pointer
Object* foo(Object* bar) {…}

//2: By reference
Object& foo(Object& bar) {…}

//3: By value (just for completeness)
Object foo(Object bar) {…}

Assuming #3’s out for performance reasons (yes, I know compilers have gotten pretty good at this, but still), the other two are more or less equivalent.

So: What’s the "best" method? Pointers? References? Some combination of the two? Or does it even matter? Technical reasons are the best, but stylistic reasons are just as good.

Update: I’ve accepted YeenFei’s answer, since it deals with the difference that clinched it for me (even if I then pointedly ignored his advice – I like having NULL as an option…). But everyone made good points – especially GMan (in the comments), and Nemo, in the answer dealing with performance and passing by value. If you’re here for answers, check them all!

  • 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-24T06:03:32+00:00Added an answer on May 24, 2026 at 6:03 am

    I would suggest to pass your argument by reference if it is expected to be valid. This would be a by-design optimization and save you from defensive programming.

    Reference cannot be null while pointer can.
    If you are dealing with pointer, you will need to verify whether given pointer is valid (non-null) regardless it is in raw form or wrapped in managed container (shared_ptr), before using them.

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

Sidebar

Related Questions

This is something curious that I saw in my coding today. Here is the
For whatever reason, our company has a coding guideline that states: Each class shall
While coding today, I noticed something odd with timespans and formatting strings. I was
I'm coding a Vector class in C# and felt that indexers would be a
I am coding a class that compares the files of two directories via comparing
I just finished coding a class, and realized that all of its public functionality
I started coding with groovy today and I notices that if I take the
Hi i made some changes in windowservice coding side(some class files related to that),,means
This is a challenging one that got me stumped while I was coding today.
I'm coding a class that's supposed to model airplane cargo and I'm having trouble

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.