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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:37:45+00:00 2026-06-14T02:37:45+00:00

When you do this: int square(int& x) { return x*x;}; int s = square(40);

  • 0

When you do this:

int square(int& x) { return x*x;};

int s = square(40); // This gives error

To fix this you do this:

int square(const int& x) { return x*x;};

int s = square(40); // This is OK

I understand that 40 is a constant but so what if I do this:

const int& x = 40 

Why would that be okay only with const keyword? Is that the way the compiler protect that no one can change the value referred to by x?

40 is a const so we don’t even know its location in memory, but shouldn’t the compiler know this address, therefore changing the value from 40 to 30 for example should be allowed since the compiler can just go to address &40 and change the value to 30?

  • 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-06-14T02:37:47+00:00Added an answer on June 14, 2026 at 2:37 am

    You’re confusing constants and literals. They are similar, but not equivalent. 40 is not a constant, it’s a literal.

    You can’t pass a literal by reference, since if you pass something by reference, it means it can be modified – literals cannot. Consider the following:

    void foo(int &i)
    {
        i = 1;
    }
    
    foo(0); // What on Earth? 0 == 1?
    

    If you, however, pass a reference to a constant, it means that even if it’s a reference, the function is not permitted to modify its argument (since it’s a constant), so now you can safely pass in a literal – it now makes sense, since there’s no possibility for the function modifying its argument.

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

Sidebar

Related Questions

It should turn this int Yada (int yada) { return yada; } into this
I have a code that looks like this: int main () { fstream file;
In a C++ function like this: int& getNumber(); what does the & mean? Is
This code gives the smallest divisor of an integer. But the problem is I
I have this SphereInFrustrum function here: 0.49% int FrustumG::sphereInFrustum(Vec3 &p, float radius) { int
my code is like this int dateColumn = c.getColumnIndex(android.provider.CallLog.Calls.DATE); long callDate = c.getInt(dateColumn); SimpleDateFormat
I am working on a loop like this: int arrA[BIG], arrB[BIG], arrC[BIG]; for(int =
I have declared a variable like this @{ int i = 1; } Now,
The code without fission looks like this: int check(int * res, char * map,
Let's say I declare this: int [a][b][c]; Does a stand for the level/page, b

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.