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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:51:59+00:00 2026-05-15T17:51:59+00:00

Can I overload a function which takes either a reference or variable name? For

  • 0

Can I overload a function which takes either a reference or variable name?

For example when I try to do this:

void function(double a);
void function(double &a);

I would like the caller of this function to be able to do:

double a = 2.5;
function(a); // should call function(double &a)
function(2.3); // should call function(double a)

I would like to write pass-by-reference functions for better memory use and possible manipulation of the variable outside of scope, but without having to create a new variable just so I can call the function.

Is this possible?

Cheers

  • 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-15T17:52:00+00:00Added an answer on May 15, 2026 at 5:52 pm

    I think you’re missing the point here. What you really should have is JUST this:

    void function(const double &a);
    

    Note the “const”. With that, you should always get pass-by-reference. If you have non-const pass by reference, then the compiler will correctly assume that you wish to modify the passed object – which of course is conceptually incompatible with the pass-by-value variant.

    With const references, the compiler will happily create the temporary object for you behind your back. The non-const version doesn’t work for you, because the compiler can only create these temporaries as “const” objects.

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

Sidebar

Ask A Question

Stats

  • Questions 522k
  • Answers 522k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This symptom of something DOM-related not working, but starting to… May 16, 2026 at 9:24 pm
  • Editorial Team
    Editorial Team added an answer It depends on where your authentication logic resides. If you… May 16, 2026 at 9:24 pm
  • Editorial Team
    Editorial Team added an answer I figured out the solution from some of the comments… May 16, 2026 at 9:24 pm

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

It must be something specific in my code, which I can't post. But maybe
I have a class which has a constructor that takes a const char* .
This question also applies to boost::function and std::tr1::function . std::function is not equality comparable:
How can I overload actions in ASP.NET MVC, but with support for GET QueryString?
How can I extend (or overload) Contains method with some custom property in a
I've been working on getting this program complete where it saves multiple structs to
This is a C# question, but really could be language-agnostic I have inherited a
As you can see, the schema.Elements returns three elements one of whom is an
Is it possible to overload how a type resolves properties when cast to dynamic?
Related: what can I use as std::map keys? I needed to create a mapping

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.