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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:01:16+00:00 2026-06-08T21:01:16+00:00

Suppose to have the code #include <iostream> struct A{ int y; int& x; A():y(0),x(y){}

  • 0

Suppose to have the code

#include <iostream>

struct A{
  int y;
  int& x;
  A():y(0),x(y){}
};



void f(int& x){
  x++;
}

void g(const A& a){
  f(a.x);
  //f(a.y);
}


int main(){
  A a;
  g(a);
  std::cout<<a.y<<std::endl;

}

inside g()
calling f() on y is not allowed because a is passed with the const modifier
however, the value of y can be modified inside g() by calling f on x;

With pointers, a similar things can be obtained very similarly

struct B{
  int* x;  
}

and

g(const B&){
   *x++;
}

is allowed. This is perfectly clear: We have a const pointer to non-const int.
But in the previous reference example, if references ARE the object, why do in this case they behave as pointers? What are the design policies under this behavior?

  • 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-08T21:01:18+00:00Added an answer on June 8, 2026 at 9:01 pm

    The actual language in the standard is

    c++11

    8.3.2 References [dcl.ref]

    1 – […]
    [ Note: A reference can be thought of as a name of an object. —end note ]

    So if a reference is a name of an object, not an object itself, then it makes sense that causing the enclosing structure to be const makes the name const (which is meaningless, as references can’t be rebound), but not the object itself.

    We also see

    6 –
    If a typedef, a type template-parameter, or a decltype-specifier denotes a type TR
    that is a reference to a type T, an attempt to create the type “lvalue reference to cv TR” creates the type “lvalue reference to T” […]

    Since member access on a cv object creates a cv lvalue, the same applies and the cv qualification is extinguished.

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

Sidebar

Related Questions

Suppose we have the following code: #include <iostream> struct A { virtual void f()
I have this example code that doesn't compile: #include <iostream> #include <vector> using std::endl;
Suppose i have a php file named main.php It Contains following code include 'config.php';
After writing the code below: #include <iostream> using namespace std; typedef struct Node {
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have code something like this: #include boost/thread/mutex.hpp using boost::mutex; typedef mutex::scoped_lock lock;
Suppose you have private static const INCLUDE_MY_DEBUG_CODE:Boolean = false; public function runMyDebugCode():void { if
Suppose that we have the following bit of code: #include <pthread.h> #include <stdio.h> #include
Suppose I have two classed Base and Derived , i.e.: #include <iostream> class Base
I have the following code taken from cplusplus.com : // set_terminate example #include <iostream>

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.