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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:11:17+00:00 2026-05-24T13:11:17+00:00

I understand the syntax and general semantics of pointers versus references, but how should

  • 0

I understand the syntax and general semantics of pointers versus references, but how should I decide when it is more-or-less appropriate to use references or pointers in an API?

Naturally some situations need one or the other (operator++ needs a reference argument), but in general I’m finding I prefer to use pointers (and const pointers) as the syntax is clear that the variables are being passed destructively.

E.g. in the following code:

void add_one(int& n) { n += 1; }
void add_one(int* const n) { *n += 1; }
int main() {
  int a = 0;
  add_one(a); // Not clear that a may be modified
  add_one(&a); // 'a' is clearly being passed destructively
}

With the pointer, it’s always (more) obvious what’s going on, so for APIs and the like where clarity is a big concern are pointers not more appropriate than references? Does that mean references should only be used when necessary (e.g. operator++)? Are there any performance concerns with one or the other?

EDIT (OUTDATED)

Besides allowing NULL values and dealing with raw arrays, it seems the choice comes down to personal preference. I’ve accepted the answer below that references Google’s C++ Style Guide, as they present the view that "References can be confusing, as they have value syntax but pointer semantics.".

Due to the additional work required to sanitise pointer arguments that should not be NULL (e.g. add_one(0) will call the pointer version and break during runtime), it makes sense from a maintainability perspective to use references where an object MUST be present, though it is a shame to lose the syntactic clarity.

  • 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-24T13:11:18+00:00Added an answer on May 24, 2026 at 1:11 pm

    Use references wherever you can, and pointers wherever you must.

    Avoid pointers until you can’t.

    The reason is that pointers make things harder to follow/read, less safe and far more dangerous manipulations than any other constructs.

    So the rule of thumb is to use pointers only if there is no other choice.

    For example, returning a pointer to an object is a valid option when the function can return nullptr in some cases and it is assumed it will. That said, a better option would be to use something similar to std::optional (requires C++17; before that, there’s boost::optional).

    Another example is to use pointers to raw memory for specific memory manipulations. That should be hidden and localized in very narrow parts of the code, to help limit the dangerous parts of the whole code base.

    In your example, there is no point in using a pointer as an argument because:

    1. if you provide nullptr as the argument, you’re going in undefined-behaviour-land;
    2. the reference attribute version doesn’t allow (without easy-to-spot tricks) the problem with 1.
    3. the reference attribute version is simpler to understand for the user: you have to provide a valid object, not something that could be null.

    If the behaviour of the function would have to work with or without a given object, then using a pointer as an attribute suggests that you can pass nullptr as the argument and it is fine for the function. That’s kind of a contract between the user and the implementation.

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

Sidebar

Related Questions

I understand the overall meaning of pointers and references(or at least I think i
Brief googling looks like you're supposed to use MarkupBuilder, but I don't understand it.
I'm trying to understand the syntax for calling use to load a library: (use
I understand what System.WeakReference does, but what I can't seem to grasp is a
I understand that some countries have laws regarding website accessibility. In general, what are
I understand the difference between String and StringBuilder ( StringBuilder being mutable) but is
I understand that they are both supposed to be small, but what are the
I am new to c# and do not understand the syntax of invoking a
I'm trying to understand the syntax of how to put together a JavaScript function
I was hoping someone could help me understand the syntax of blocks when used

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.