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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:02:42+00:00 2026-05-12T16:02:42+00:00

I am programming in C++ more then 5 years, and have never met any

  • 0

I am programming in C++ more then 5 years, and have never met any place where reference of the variable is recommended to use except as a function argument (if you don’t want to copy what you pass as your function argument). So could someone point cases where C++ variable reference is recommended (I mean it gives any advantage) to use.

  • 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-12T16:02:42+00:00Added an answer on May 12, 2026 at 4:02 pm

    As a return value of an opaque collection accessor/mutator

    The operator[] of std::map returns a reference.


    To shorten the text needed to reference a variable

    If you miss old-school with Foo do ... statement (that’s Pascal syntax), you can write

     MyString &name = a->very->long_->accessor->to->member;
     if (name.upcase() == "JOHN") {
        name += " Smith";
     }
    

    another example of this can be found in Mike Dunlavey’s answer


    To state that something is just a reference

    References are also useful in wrapper objects and functors–i.e. in intermediate objects that logically contact no members but only references to them.

    Example:

    class User_Filter{
      std::list<User> const& stop_list;
      public: Functor (std::list<User> const& lst)
        : stop_list(lst) { }
      public: bool operator()(User const& u) const
        {  return stop_list.exists(u); }
    };
    
    find_if(x.begin(),x.end(),User_Filter(user_list));
    

    The idea here that it’s a compile error if you don’t initialize a reference in constructor of such an object. The more checks in compile time–the better programs are.

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

Sidebar

Related Questions

I have a few years experience programming c++ and a little less then that
When programming, I need a way to have more than one clipboard (Vista). I
We have been using Doug Lea's concurrent library for more than 8 years now.
I've studied C programming in college some years ago and have developed some medium
I am studying More programming pearls (Addison-Wesley, 1988) by Bentley J. and am stumped
I am interested in understanding object-oriented programming in a more academic and abstract way
This is more of a maths question than programming but I figure a lot
This question is more UI/Design-ish than hard-core programming is. Background: I've been coding in
I am trying to get more adept and my C programming and I was
I succesfully used a validator more than once,but after some programming my validators are

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.