Why is `const int& k = i; ++i; ` possible? [duplicate]












10
















This question already has an answer here:




  • Why can const int& bind to an int?

    8 answers



  • A const & refers to a nonvolatile variable. The variable changes. Does the change invalidate the const &?

    4 answers




I am supposed to determine whether this function is syntactically correct:



int f3(int i, int j) { const int& k=i; ++i; return k; }



I have tested it out and it compiles with my main function.



I do not understand why this is so.



Surely by calling the function f3 I create copies of the variables iand j in a new memory space and setting const int& k=i I am setting the memory space of the newly created k to the exact the same space of the memory space of the copied i, therefore any change, i.e. the increment ++iwill result in ++k which is not possible given that it was set const



Any help is greatly appreciated










share|improve this question













marked as duplicate by Cody Gray c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Small nitpicking: int f3(int i, int j) { const int& k=i; ++k; return k; } would be syntactically correct too

    – danielspaniol
    2 days ago











  • @danielspaniol I get error: cannot assign to variable 'k' with const-qualified type 'const int &'

    – Thomas Sablik
    2 days ago











  • @ThomasSablik That is a semantical error. The syntax is correct.

    – danielspaniol
    2 days ago











  • I am setting the memory space of the newly created k. No. K is a reference, this means is equivalent to an alias (an alias is literally another another name for a variable). So k does not need its own variable it is simply another name for i (though it is const qualified). You can verify that it does not have its own memory because you can not take the address of k, if you try you will get the address of i.

    – Martin York
    yesterday


















10
















This question already has an answer here:




  • Why can const int& bind to an int?

    8 answers



  • A const & refers to a nonvolatile variable. The variable changes. Does the change invalidate the const &?

    4 answers




I am supposed to determine whether this function is syntactically correct:



int f3(int i, int j) { const int& k=i; ++i; return k; }



I have tested it out and it compiles with my main function.



I do not understand why this is so.



Surely by calling the function f3 I create copies of the variables iand j in a new memory space and setting const int& k=i I am setting the memory space of the newly created k to the exact the same space of the memory space of the copied i, therefore any change, i.e. the increment ++iwill result in ++k which is not possible given that it was set const



Any help is greatly appreciated










share|improve this question













marked as duplicate by Cody Gray c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Small nitpicking: int f3(int i, int j) { const int& k=i; ++k; return k; } would be syntactically correct too

    – danielspaniol
    2 days ago











  • @danielspaniol I get error: cannot assign to variable 'k' with const-qualified type 'const int &'

    – Thomas Sablik
    2 days ago











  • @ThomasSablik That is a semantical error. The syntax is correct.

    – danielspaniol
    2 days ago











  • I am setting the memory space of the newly created k. No. K is a reference, this means is equivalent to an alias (an alias is literally another another name for a variable). So k does not need its own variable it is simply another name for i (though it is const qualified). You can verify that it does not have its own memory because you can not take the address of k, if you try you will get the address of i.

    – Martin York
    yesterday
















10












10








10


1







This question already has an answer here:




  • Why can const int& bind to an int?

    8 answers



  • A const & refers to a nonvolatile variable. The variable changes. Does the change invalidate the const &?

    4 answers




I am supposed to determine whether this function is syntactically correct:



int f3(int i, int j) { const int& k=i; ++i; return k; }



I have tested it out and it compiles with my main function.



I do not understand why this is so.



Surely by calling the function f3 I create copies of the variables iand j in a new memory space and setting const int& k=i I am setting the memory space of the newly created k to the exact the same space of the memory space of the copied i, therefore any change, i.e. the increment ++iwill result in ++k which is not possible given that it was set const



Any help is greatly appreciated










share|improve this question















This question already has an answer here:




  • Why can const int& bind to an int?

    8 answers



  • A const & refers to a nonvolatile variable. The variable changes. Does the change invalidate the const &?

    4 answers




I am supposed to determine whether this function is syntactically correct:



int f3(int i, int j) { const int& k=i; ++i; return k; }



I have tested it out and it compiles with my main function.



I do not understand why this is so.



Surely by calling the function f3 I create copies of the variables iand j in a new memory space and setting const int& k=i I am setting the memory space of the newly created k to the exact the same space of the memory space of the copied i, therefore any change, i.e. the increment ++iwill result in ++k which is not possible given that it was set const



Any help is greatly appreciated





This question already has an answer here:




  • Why can const int& bind to an int?

    8 answers



  • A const & refers to a nonvolatile variable. The variable changes. Does the change invalidate the const &?

    4 answers








c++ syntax reference const






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









user9078057user9078057

1319




1319




marked as duplicate by Cody Gray c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Cody Gray c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    Small nitpicking: int f3(int i, int j) { const int& k=i; ++k; return k; } would be syntactically correct too

    – danielspaniol
    2 days ago











  • @danielspaniol I get error: cannot assign to variable 'k' with const-qualified type 'const int &'

    – Thomas Sablik
    2 days ago











  • @ThomasSablik That is a semantical error. The syntax is correct.

    – danielspaniol
    2 days ago











  • I am setting the memory space of the newly created k. No. K is a reference, this means is equivalent to an alias (an alias is literally another another name for a variable). So k does not need its own variable it is simply another name for i (though it is const qualified). You can verify that it does not have its own memory because you can not take the address of k, if you try you will get the address of i.

    – Martin York
    yesterday
















  • 1





    Small nitpicking: int f3(int i, int j) { const int& k=i; ++k; return k; } would be syntactically correct too

    – danielspaniol
    2 days ago











  • @danielspaniol I get error: cannot assign to variable 'k' with const-qualified type 'const int &'

    – Thomas Sablik
    2 days ago











  • @ThomasSablik That is a semantical error. The syntax is correct.

    – danielspaniol
    2 days ago











  • I am setting the memory space of the newly created k. No. K is a reference, this means is equivalent to an alias (an alias is literally another another name for a variable). So k does not need its own variable it is simply another name for i (though it is const qualified). You can verify that it does not have its own memory because you can not take the address of k, if you try you will get the address of i.

    – Martin York
    yesterday










1




1





Small nitpicking: int f3(int i, int j) { const int& k=i; ++k; return k; } would be syntactically correct too

– danielspaniol
2 days ago





Small nitpicking: int f3(int i, int j) { const int& k=i; ++k; return k; } would be syntactically correct too

– danielspaniol
2 days ago













@danielspaniol I get error: cannot assign to variable 'k' with const-qualified type 'const int &'

– Thomas Sablik
2 days ago





@danielspaniol I get error: cannot assign to variable 'k' with const-qualified type 'const int &'

– Thomas Sablik
2 days ago













@ThomasSablik That is a semantical error. The syntax is correct.

– danielspaniol
2 days ago





@ThomasSablik That is a semantical error. The syntax is correct.

– danielspaniol
2 days ago













I am setting the memory space of the newly created k. No. K is a reference, this means is equivalent to an alias (an alias is literally another another name for a variable). So k does not need its own variable it is simply another name for i (though it is const qualified). You can verify that it does not have its own memory because you can not take the address of k, if you try you will get the address of i.

– Martin York
yesterday







I am setting the memory space of the newly created k. No. K is a reference, this means is equivalent to an alias (an alias is literally another another name for a variable). So k does not need its own variable it is simply another name for i (though it is const qualified). You can verify that it does not have its own memory because you can not take the address of k, if you try you will get the address of i.

– Martin York
yesterday














1 Answer
1






active

oldest

votes


















22















the increment ++iwill result in ++k which is not possible given that it was set const




That's a misunderstanding.



You may not change the value of the object through k but it can still be changed through other means. In other words, ++k is not allowed but ++i is still allowed, which will indirectly modify the value of k.



Here's an analogy from a non-computer world.



You may look through the window of a store and see what's inside but you won't be able to change what's inside the store. However, an employee, who is inside the store, can change the contents of the store. You will see that change from
outside. You have const access or view access to the store while the employee has non-const access or change access to the store.






share|improve this answer





















  • 7





    Not so far-fetched. That's a good analogy.

    – user4581301
    2 days ago











  • Note that this is so because i itself is not const. So we have a const reference a non-const object. Const objects are different.

    – David Schwartz
    2 days ago











  • @DavidSchwartz This is true, but now that I think of it, it doesn't suit the logic for pointers, does it? since const int * would be a non-constant pointer to a constant int and int * const would be a constant pointer to a non-constant int, this should be int & const, shouldn't it? I feel like this is inconsistent.

    – Max
    2 days ago


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









22















the increment ++iwill result in ++k which is not possible given that it was set const




That's a misunderstanding.



You may not change the value of the object through k but it can still be changed through other means. In other words, ++k is not allowed but ++i is still allowed, which will indirectly modify the value of k.



Here's an analogy from a non-computer world.



You may look through the window of a store and see what's inside but you won't be able to change what's inside the store. However, an employee, who is inside the store, can change the contents of the store. You will see that change from
outside. You have const access or view access to the store while the employee has non-const access or change access to the store.






share|improve this answer





















  • 7





    Not so far-fetched. That's a good analogy.

    – user4581301
    2 days ago











  • Note that this is so because i itself is not const. So we have a const reference a non-const object. Const objects are different.

    – David Schwartz
    2 days ago











  • @DavidSchwartz This is true, but now that I think of it, it doesn't suit the logic for pointers, does it? since const int * would be a non-constant pointer to a constant int and int * const would be a constant pointer to a non-constant int, this should be int & const, shouldn't it? I feel like this is inconsistent.

    – Max
    2 days ago
















22















the increment ++iwill result in ++k which is not possible given that it was set const




That's a misunderstanding.



You may not change the value of the object through k but it can still be changed through other means. In other words, ++k is not allowed but ++i is still allowed, which will indirectly modify the value of k.



Here's an analogy from a non-computer world.



You may look through the window of a store and see what's inside but you won't be able to change what's inside the store. However, an employee, who is inside the store, can change the contents of the store. You will see that change from
outside. You have const access or view access to the store while the employee has non-const access or change access to the store.






share|improve this answer





















  • 7





    Not so far-fetched. That's a good analogy.

    – user4581301
    2 days ago











  • Note that this is so because i itself is not const. So we have a const reference a non-const object. Const objects are different.

    – David Schwartz
    2 days ago











  • @DavidSchwartz This is true, but now that I think of it, it doesn't suit the logic for pointers, does it? since const int * would be a non-constant pointer to a constant int and int * const would be a constant pointer to a non-constant int, this should be int & const, shouldn't it? I feel like this is inconsistent.

    – Max
    2 days ago














22












22








22








the increment ++iwill result in ++k which is not possible given that it was set const




That's a misunderstanding.



You may not change the value of the object through k but it can still be changed through other means. In other words, ++k is not allowed but ++i is still allowed, which will indirectly modify the value of k.



Here's an analogy from a non-computer world.



You may look through the window of a store and see what's inside but you won't be able to change what's inside the store. However, an employee, who is inside the store, can change the contents of the store. You will see that change from
outside. You have const access or view access to the store while the employee has non-const access or change access to the store.






share|improve this answer
















the increment ++iwill result in ++k which is not possible given that it was set const




That's a misunderstanding.



You may not change the value of the object through k but it can still be changed through other means. In other words, ++k is not allowed but ++i is still allowed, which will indirectly modify the value of k.



Here's an analogy from a non-computer world.



You may look through the window of a store and see what's inside but you won't be able to change what's inside the store. However, an employee, who is inside the store, can change the contents of the store. You will see that change from
outside. You have const access or view access to the store while the employee has non-const access or change access to the store.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered 2 days ago









R SahuR Sahu

170k1294193




170k1294193








  • 7





    Not so far-fetched. That's a good analogy.

    – user4581301
    2 days ago











  • Note that this is so because i itself is not const. So we have a const reference a non-const object. Const objects are different.

    – David Schwartz
    2 days ago











  • @DavidSchwartz This is true, but now that I think of it, it doesn't suit the logic for pointers, does it? since const int * would be a non-constant pointer to a constant int and int * const would be a constant pointer to a non-constant int, this should be int & const, shouldn't it? I feel like this is inconsistent.

    – Max
    2 days ago














  • 7





    Not so far-fetched. That's a good analogy.

    – user4581301
    2 days ago











  • Note that this is so because i itself is not const. So we have a const reference a non-const object. Const objects are different.

    – David Schwartz
    2 days ago











  • @DavidSchwartz This is true, but now that I think of it, it doesn't suit the logic for pointers, does it? since const int * would be a non-constant pointer to a constant int and int * const would be a constant pointer to a non-constant int, this should be int & const, shouldn't it? I feel like this is inconsistent.

    – Max
    2 days ago








7




7





Not so far-fetched. That's a good analogy.

– user4581301
2 days ago





Not so far-fetched. That's a good analogy.

– user4581301
2 days ago













Note that this is so because i itself is not const. So we have a const reference a non-const object. Const objects are different.

– David Schwartz
2 days ago





Note that this is so because i itself is not const. So we have a const reference a non-const object. Const objects are different.

– David Schwartz
2 days ago













@DavidSchwartz This is true, but now that I think of it, it doesn't suit the logic for pointers, does it? since const int * would be a non-constant pointer to a constant int and int * const would be a constant pointer to a non-constant int, this should be int & const, shouldn't it? I feel like this is inconsistent.

– Max
2 days ago





@DavidSchwartz This is true, but now that I think of it, it doesn't suit the logic for pointers, does it? since const int * would be a non-constant pointer to a constant int and int * const would be a constant pointer to a non-constant int, this should be int & const, shouldn't it? I feel like this is inconsistent.

– Max
2 days ago





Popular posts from this blog

Category:香港粉麵

List *all* the tuples!

Channel [V]