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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:14:52+00:00 2026-05-11T12:14:52+00:00

In a project I maintain, I see a lot of code like this for

  • 0

In a project I maintain, I see a lot of code like this for simple get/set methods

const int & MyClass::getFoo() { return m_foo; }  void MyClass::setFoo(const int & foo) { m_foo = foo; } 

What is the point in doing that instead of the following?

int MyClass::getFoo() { return m_foo; }  // Removed 'const' and '&'   void MyClass::setFoo(const int foo) { m_foo = foo; }  // Removed '&' 

Passing a reference to a primitive type should require the same (or more) effort as passing the type’s value itself, right?
It’s just a number after all…
Is this just some attempted micro-optimization or is there a true benefit?

  • 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. 2026-05-11T12:14:53+00:00Added an answer on May 11, 2026 at 12:14 pm

    The difference is that if you get that result into a reference yourself you can track the changes of the integer member variable in your own variable name without recalling the function.

    const &int x = myObject.getFoo(); cout<<x<<endl; //... cout<<x<<endl;//x might have changed 

    It’s probably not the best design choice, and it’s very dangerous to return a reference (const or not), in case a variable that gets freed from scope is returned. So if you return a reference, be careful to be sure it is not a variable that goes out of scope.

    There is a slight difference for the modifier too, but again probably not something that is worth doing or that was intended.

    void test1(int x) {   cout<<x<<endl;//prints 1 }  void test2(const int &x) {   cout<<x<<endl;//prints 1 or something else possibly, another thread could have changed x }  int main(int argc, char**argv) {   int x = 1;   test1(x);   //...   test2(x);   return 0; } 

    So the end result is that you obtain changes even after the parameters are passed.

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

Sidebar

Related Questions

I got a task to maintain a c# project, I found some code like
In a recent project, I have to maintain some PHP code. I set up
so I need to maintain this old legacy project, where one part of it
I want to set up an Eclipse project on my Mac and maintain the
Is there a way i can see stats on my class design? This project
In the following code you'll see a simple lexer that conforms to the following
I have a project where I maintain a list of all my students and
There is a project that I need to maintain that talks to mysql via
I'm working on a small (java) project where a website needs to maintain a
I see a huge number of request and response types in a project to

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.