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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:34:49+00:00 2026-05-14T05:34:49+00:00

From what I understand: when you pass by value, the function makes a local

  • 0

From what I understand: when you pass by value, the function makes a local copy of the passed argument and uses that; when the function ends, it goes out of scope. When you pass by const reference, the function uses a reference to the passed argument that can’t be modified. I don’t understand, however, why one would choose one over the other, except in a situation where an argument needs to be modified and returned. If you had a void function where nothing is getting returned, why choose one over the other?

EDIT: So basically passing by const reference avoids copying the object. So in what situations is copying the object good? I mean, why not just use const references all the time if it optimizes performance all the time?

  • 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-14T05:34:50+00:00Added an answer on May 14, 2026 at 5:34 am

    There are two main considerations. One is the expense of copying the passed object and the second is the assumptions that the compiler can make when the object is a a local object.

    E.g. In the first form, in the body of f it cannot be assumed that a and b don’t reference the same object; so the value of a must be re-read after any write to b, just in case. In the second form, a cannot be changed via a write to b, as it is local to the function, so these re-reads are unnecessary.

    void f(const Obj& a, Obj& b)
    {
        // a and b could reference the same object
    }
    
    void f(Obj a, Obj& b)
    {
        // a is local, b cannot be a reference to a
    }
    

    E.g.: In the first example, the compiler may be able to assume that the value of a local object doesn’t change when an unrelated call is made. Without information about h, the compiler may not know whether an object that that function has a reference to (via a reference parameter) isn’t changed by h. For example, that object might be part of a global state which is modified by h.

    void g(const Obj& a)
    {
        // ...
        h(); // the value of a might change
        // ...
    }
    
    void g(Obj a)
    {
        // ...
        h(); // the value of a is unlikely to change
        // ...
    }
    

    Unfortunately, this example isn’t cast iron. It is possible to write a class that, say, adds a pointer to itself to a global state object in its constructor, so that even a local object of class type might be altered by a global function call. Despite this, there are still potentially more opportunities for valid optimizations for local objects as they can’t be aliased directly by references passed in, or other pre-existing objects.

    Passing a parameter by const reference should be chosen where the semantics of references are actually required, or as a performance improvement only if the cost of potential aliasing would be outweighed by the expense of copying the parameter.

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

Sidebar

Ask A Question

Stats

  • Questions 495k
  • Answers 495k
  • 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 The recommended way to do this is with a system… May 16, 2026 at 11:18 am
  • Editorial Team
    Editorial Team added an answer I would recommend Poedit, which produces .po file, which is… May 16, 2026 at 11:18 am
  • Editorial Team
    Editorial Team added an answer Did you mean strrchr()? I would probably approach this by… May 16, 2026 at 11:18 am

Trending Tags

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

Top Members

Related Questions

I understand that functions can have attributes. So I can do the following: def
I want to make a constructor function that creates a documentElement object. As an
I read from the jQuery website, that get() loads data from the server using
$.post('testeUpdate.php', 'autocomplete', function(dadosResposta) { $('#ns-nome').val(dadosResposta.nsName); $('#ns-endereco').val(dadosResposta.nsAddress); }, json); I'm trying to understand this. So,
I need to find a way to call a vb.net function in my aspx
I am really new to Haskell (Actually I saw Real World Haskell from O'Reilly
I'm a python newbie and I don't understand why it won't read my IP
I have an iPhone VOIP app that copes with multi-multi transmit and receive (ie
I have wrote universal script to get all rows from Doctrine Table Models, but
I am using ASP.NET MVC application. However I am not able to understand the

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.