Why is `const int& k = i; ++i; ` possible?What is the difference between const and readonly?How to convert a std::string to const char* or char*?Why can templates only be implemented in the header file?Meaning of 'const' last in a function declaration of a class?What is the difference between const int*, const int * const, and int const *?Why is “using namespace std” considered bad practice?define() vs constWhy does ++[[]][+[]]+[+[]] return the string “10”?Why are elementwise additions much faster in separate loops than in a combined loop?Why is it faster to process a sorted array than an unsorted array?
What to do with wrong results in talks?
Your magic is very sketchy
Valid Badminton Score?
Will it be accepted, if there is no ''Main Character" stereotype?
Is the destination of a commercial flight important for the pilot?
At which point does a character regain all their Hit Dice?
Is it okay / does it make sense for another player to join a running game of Munchkin?
What does this 7 mean above the f flat
Applicability of Single Responsibility Principle
The baby cries all morning
How do I keep an essay about "feeling flat" from feeling flat?
Is this Spell Mimic feat balanced?
Is there any reason not to eat food that's been dropped on the surface of the moon?
Hide Select Output from T-SQL
Curses work by shouting - How to avoid collateral damage?
Efficiently merge handle parallel feature branches in SFDX
Go Pregnant or Go Home
Personal Teleportation as a Weapon
What would happen if the UK refused to take part in EU Parliamentary elections?
Confused about a passage in Harry Potter y la piedra filosofal
Why is `const int& k = i; ++i; ` possible?
Is expanding the research of a group into machine learning as a PhD student risky?
apt-get update is failing in debian
How does residential electricity work?
Why is `const int& k = i; ++i; ` possible?
What is the difference between const and readonly?How to convert a std::string to const char* or char*?Why can templates only be implemented in the header file?Meaning of 'const' last in a function declaration of a class?What is the difference between const int*, const int * const, and int const *?Why is “using namespace std” considered bad practice?define() vs constWhy does ++[[]][+[]]+[+[]] return the string “10”?Why are elementwise additions much faster in separate loops than in a combined loop?Why is it faster to process a sorted array than an unsorted array?
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 i
and 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 ++i
will result in ++k
which is not possible given that it was set const
Any help is greatly appreciated
c++ syntax reference const
add a comment |
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 i
and 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 ++i
will result in ++k
which is not possible given that it was set const
Any help is greatly appreciated
c++ syntax reference const
add a comment |
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 i
and 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 ++i
will result in ++k
which is not possible given that it was set const
Any help is greatly appreciated
c++ syntax reference const
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 i
and 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 ++i
will result in ++k
which is not possible given that it was set const
Any help is greatly appreciated
c++ syntax reference const
c++ syntax reference const
asked 7 hours ago
user9078057user9078057
1118
1118
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 a far-fetched analogy.
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.
5
Not so far-fetched. That's a good analogy.
– user4581301
7 hours 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
6 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367062%2fwhy-is-const-int-k-i-i-possible%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 a far-fetched analogy.
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.
5
Not so far-fetched. That's a good analogy.
– user4581301
7 hours 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
6 hours ago
add a comment |
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 a far-fetched analogy.
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.
5
Not so far-fetched. That's a good analogy.
– user4581301
7 hours 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
6 hours ago
add a comment |
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 a far-fetched analogy.
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.
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 a far-fetched analogy.
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.
edited 7 hours ago
answered 7 hours ago
R SahuR Sahu
169k1294193
169k1294193
5
Not so far-fetched. That's a good analogy.
– user4581301
7 hours 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
6 hours ago
add a comment |
5
Not so far-fetched. That's a good analogy.
– user4581301
7 hours 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
6 hours ago
5
5
Not so far-fetched. That's a good analogy.
– user4581301
7 hours ago
Not so far-fetched. That's a good analogy.
– user4581301
7 hours 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
6 hours 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
6 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367062%2fwhy-is-const-int-k-i-i-possible%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown