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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:38:18+00:00 2026-05-26T20:38:18+00:00

Just a quick question about a function like this: class Test { public: Test(vector<int>&

  • 0

Just a quick question about a function like this:

class Test {
  public:
    Test(vector<int>& v) {
      v_ = v;
    }
  private:
    std::vector<int> v_;
};

What’s the difference between using Test(vector<int>& v) and Test(vector<int> v)? I seem to know that the first one should be faster since it is pass-by-reference. But I’m not so sure whether there are other differences.

  • 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-26T20:38:19+00:00Added an answer on May 26, 2026 at 8:38 pm

    The difference is that with Test(vector<int>& v) (which BTW is an lvalue reference) v refers to the original object, while with Test(vector<int> v) you have a copy. The following example code demonstrates the difference with an int and a normal function (note that for int, pass-by-value is actually faster!):

    #include <iostream>
    
    int global_i;
    
    void pass_by_value(int i)
    {
      std::cout << "pass by value:\n";
      std::cout << "initially: i = " << i << ", global_i = " << global_i << "\n";
      i++;
      std::cout << "after i++: i = " << i << ", global_i = " << global_i << "\n";
      global_i++;
      std::cout << "after global_i++: i = " << i << ", global_i = " << global_i << "\n";
    }
    
    void pass_by_reference(int& i)
    {
      std::cout << "pass by reference:\n";
      std::cout << "initially: i = " << i << ", global_i = " << global_i << "\n";
      i++;
      std::cout << "after i++: i = " << i << ", global_i = " << global_i << "\n";
      global_i++;
      std::cout << "after global_i++: i = " << i << ", global_i = " << global_i << "\n";
    }
    
    void pass_by_const_reference(int const& i)
    {
      std::cout << "pass by const reference:\n";
      std::cout << "initially: i = " << i << ", global_i = " << global_i << "\n";
      // i++; not allowed!
      // std::cout << "after i++: i = " << i << ", global_i = " << global_i << "\n";
      global_i++;
      std::cout << "after global_i++: i = " << i << ", global_i = " << global_i << "\n";
    }
    
    int main()
    {
      global_i = 1;
      pass_by_value(global_i);
    
      global_i = 1;
      pass_by_reference(global_i);
    
      global_i = 1;
      pass_by_const_reference(global_i);
    }
    

    The output of this is:

    pass by value:
    initially: i = 1, global_i = 1
    after i++: i = 2, global_i = 1
    after global_i++: i = 2, global_i = 2
    pass by reference:
    initially: i = 1, global_i = 1
    after i++: i = 2, global_i = 2
    after global_i++: i = 3, global_i = 3
    pass by const reference:
    initially: i = 1, global_i = 1
    after global_i++: i = 2, global_i = 2
    

    As you see, with call by value, the argument and the passed variable are completely separate. Incrementing the argument does not change the passed variable, and incrementing the passed variable does not change the argument. On the other hand, with pass by reference, the argument just gives access to the passed variable: It doesn’t matter which one you increment because effectively, they are the same. With pass by const reference, they are also the same, but you are not allowed to mosify the argument (there are ways around this, though). However, the argument still reflects any changes to the passed variable.

    Those are the differences in functionality. However there are some more differences: For pass by value and pass by const reference, you can use an rvalue, like call_by_value(2) or call_by_const_reference(2). For call by value, it’s obvious what happens: The argument gets the value 2, and that’s it. However for const reference, there’s an object expected (you could for example take the address of that object in the function). Therefore in that case a temporary object is created. For call by non-const reference, you cannot pass an rvalue.

    C++11 adds another type to the mix, namely rvalue references. Those are denoted with && instead of &. Inside the function they behave exactly like normal (lvalue) references, but they differ in that they can be bound to rvalues, even if they are not const. Moreover, if you use them as return type, the call expression will be of rvalue type as if you had returned a value. Especially, you would not be able to pass the result of a function returning an rvalue reference to a function expecting an lvalue reference, just like you couldn’t do that with the literal 2.

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

Sidebar

Related Questions

Just a quick question about how you would go about implementing this. I want
Just a quick question about JQuery's Ajax function, more specifically, the url parameter. My
Just a quick question about my Domain Layer/ Domain Service... Should I allow this
Just a quick question about the Timer class of Flex 4. I have a
Just a quick question about ReadWriteLocks in Java (specifically the ReentrantReadWriteLock implementation) as I
Just a quick question about how parameters are passed in Java... ... if ((index
Just a quick question about best practice in MVC development. Let's say that I've
Just a quick question about using select() . I'm using select() to read from
I just had a quick question about git - how backwards compatible are git
I just have a quick question about how to get the length of a

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.