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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:23:27+00:00 2026-06-13T03:23:27+00:00

Possible Duplicate: What are the differences between pointer variable and reference variable in C++?

  • 0

Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
What does a function prototype mean with an ampersand in it?

In my understanding,

void bar(int &x) { ... }

Seem to mean pass x by reference. But in C++, there are pointers etc already. So what difference is there to

void bar(int *x) { ... }
// then call by
bar(&x);

Apart from the fact that it’s longer… I also noticed if I use the 2nd method, I need to use -> as opposed to . if I pass in a struct… why?

  • 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-06-13T03:23:28+00:00Added an answer on June 13, 2026 at 3:23 am

    The reason Stroustrup gives for introducing references to C++ is operator overloading.

    http://www.stroustrup.com/bs_faq2.html#pointers-and-references

    In your example function bar, it is no big deal whether the user has to call it as bar(&x) (because it takes a pointer), or can call it with bar(x) (because it takes a reference). At least, C programmers think it isn’t.

    However, when operator overloading was added to C++, Stroustrup considered that using overloaded operators with pointers is very inelegant (“ugly” in his words).

    References have some advantages in functionality over pointers, such as the fact that a temporary object can be bound to a const reference, but you can’t apply the & operator to it. So a pass-by-const-reference function sometimes saves the caller a line of code (to create a variable) compared with its pass-by-pointer-to-const equivalent.

    For this reason, one possible convention is to accept a pointer when your function plans to store the address somewhere for future use after it returns, and a reference when it doesn’t. It doesn’t prevent all possible ways of creating a dangling pointer/reference, but it catches a big one. It does have unfortunate consequences when writing functional-style code, though, so it’s not for everyone.

    I also noticed if I use the 2nd method, I need to use -> as opposed to
    . if I pass in a struct… why?

    It’s just the syntax inherited from C. . to access a member of a struct, -> to access a member via a pointer-to-struct. In C you can only use -> with a pointer on the LHS, and you can never use . with a pointer on the LHS. So there’s no strict need for different symbols, it just helps make code more readable to have reminders. For example, if the same symbol . was used for both then (*ptr).member would mean the same thing as ptr.member, which would probably be confusing.

    In C++ the difference becomes useful to the language. You can overload operator-> for a class type, for example smart pointers do. But class types can have members accessed with .. So some_smart_ptr->get(); means “call the get() function on the referand of the smart pointer”, whereas some_smart_ptr.get() means “call the get() function on the smart pointer”.

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

Sidebar

Related Questions

Possible Duplicate: What are the differences between pointer variable and reference variable in C++?
Possible Duplicate: What are the differences between pointer variable and reference variable in C++?
Possible Duplicate: What are the differences between pointer variable and reference variable in C++?
Possible Duplicate: What are the differences between pointer variable and reference variable in C++?
Possible Duplicate: What are the differences between pointer variable and reference variable in C++?
Possible Duplicate: What are the differences between pointer variable and reference variable in C++?
Possible Duplicate: Difference between pointer variable and reference variable in C++ When should I
Possible Duplicate: Difference between pointer variable and reference variable in C++ I am reading
Possible Duplicate: Difference between pointer variable and reference variable in C++ As I am
Possible Duplicate: Difference between pointer variable and reference variable in C++ I saw this

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.