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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:54:15+00:00 2026-05-16T20:54:15+00:00

I think the following is really basic but I don’t really get what would

  • 0

I think the following is really basic but I don’t really get what would be the “advantages” of one of these code.

Having this:

int main()
{
    int a = 10;
    f(a);
    return 0;
}

What would be the difference between

void f(int& a)
{
    int& b = a;
}

and

void f(int& a)
{
    int b = a;
}

In particular, in the case where f would instead be the constructor of a class and where instead of an int we have a big object.

I would go with the second possibility… What do you think ?

Thanks.

Edit:

First, looking at the answers, I though I was brain dead when I posted the question, but actually I found interesting subquestions (and answers !) in the comments.

  • 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-16T20:54:15+00:00Added an answer on May 16, 2026 at 8:54 pm

    First:

    void f(int& a) {
        int& b = a;
    }
    

    The caller passes in an a, which we get as a reference to the caller’s a. Then we create a new referece b which refers to a, which is still the caller’s a. As a result, any changes the caller makes will be visible to f and its containing class, and any changes made in f to either a or b will affect what the caller passed in as a.

    void f(int& a) {
        int b = a;
    }
    

    Here, b is a new variable, an int, which takes a copy of a. Changes made to b won’t affect the caller, nor will the caller affect b. But a quicker way of writing the same thing is:

    void f(int b) {
    }
    

    and dispense with the a reference entirely. In particular, this shows in the prototype that the function will not modify its parameter, because it cannot; while a prototype of void f (int &a) indicates that it may modify a.

    Edit: if you need to avoid any copying, a Third Option ™ is a reference to const:

    void f(const int &a) {
        const int &b = a;
    }
    

    inside f, neither a nor b can be modified, so it can’t affect the caller, but copying is avoided by using a reference to const. There is still a potential problem that any changes to a caused by the caller will be visible inside the object, because there is only one actual data item a and many names for it; that’s something you’d have to document to ensure the caller does not do any such thing.

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

Sidebar

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.