Why do I get a compiler error, when applying the increment operator to a constant variable
up vote
1
down vote
favorite
If I declare a constant variable
int const n=100; cout<<n+1<<endl;
The console shows the value as 101
but when I write a code like this:
int const n=100;
n++;
cout<<n<<endl;
There is a compile time error:
main.cpp: In function 'int main()':
main.cpp:6:5: error: increment of read-only variable 'n'
Is the second case different from the first case?
c++
New contributor
|
show 3 more comments
up vote
1
down vote
favorite
If I declare a constant variable
int const n=100; cout<<n+1<<endl;
The console shows the value as 101
but when I write a code like this:
int const n=100;
n++;
cout<<n<<endl;
There is a compile time error:
main.cpp: In function 'int main()':
main.cpp:6:5: error: increment of read-only variable 'n'
Is the second case different from the first case?
c++
New contributor
1
Why did you declare it constant if you were planning to change it?
– Ayxan
2 days ago
The exact error message should be given.
– ederag
2 days ago
@ederag It's pretty obvious what the exact error message would be in case of trying to increment a constant variable. OP asks for the difference of incrementing and creation of temporary values liken+1
.
– πάντα ῥεῖ
2 days ago
@πάνταῥεῖ Sure. Just recalling good practices for a new user. Having the exact message makes the question easily reachable by searching for the error message. Remember that SO is not a forum. Good questions are useful not only to the original poster.
– ederag
2 days ago
@ederag Satisfied now?
– πάντα ῥεῖ
2 days ago
|
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
If I declare a constant variable
int const n=100; cout<<n+1<<endl;
The console shows the value as 101
but when I write a code like this:
int const n=100;
n++;
cout<<n<<endl;
There is a compile time error:
main.cpp: In function 'int main()':
main.cpp:6:5: error: increment of read-only variable 'n'
Is the second case different from the first case?
c++
New contributor
If I declare a constant variable
int const n=100; cout<<n+1<<endl;
The console shows the value as 101
but when I write a code like this:
int const n=100;
n++;
cout<<n<<endl;
There is a compile time error:
main.cpp: In function 'int main()':
main.cpp:6:5: error: increment of read-only variable 'n'
Is the second case different from the first case?
c++
c++
New contributor
New contributor
edited 2 days ago
πάντα ῥεῖ
71.1k970132
71.1k970132
New contributor
asked 2 days ago
Mudit Sharma
142
142
New contributor
New contributor
1
Why did you declare it constant if you were planning to change it?
– Ayxan
2 days ago
The exact error message should be given.
– ederag
2 days ago
@ederag It's pretty obvious what the exact error message would be in case of trying to increment a constant variable. OP asks for the difference of incrementing and creation of temporary values liken+1
.
– πάντα ῥεῖ
2 days ago
@πάνταῥεῖ Sure. Just recalling good practices for a new user. Having the exact message makes the question easily reachable by searching for the error message. Remember that SO is not a forum. Good questions are useful not only to the original poster.
– ederag
2 days ago
@ederag Satisfied now?
– πάντα ῥεῖ
2 days ago
|
show 3 more comments
1
Why did you declare it constant if you were planning to change it?
– Ayxan
2 days ago
The exact error message should be given.
– ederag
2 days ago
@ederag It's pretty obvious what the exact error message would be in case of trying to increment a constant variable. OP asks for the difference of incrementing and creation of temporary values liken+1
.
– πάντα ῥεῖ
2 days ago
@πάνταῥεῖ Sure. Just recalling good practices for a new user. Having the exact message makes the question easily reachable by searching for the error message. Remember that SO is not a forum. Good questions are useful not only to the original poster.
– ederag
2 days ago
@ederag Satisfied now?
– πάντα ῥεῖ
2 days ago
1
1
Why did you declare it constant if you were planning to change it?
– Ayxan
2 days ago
Why did you declare it constant if you were planning to change it?
– Ayxan
2 days ago
The exact error message should be given.
– ederag
2 days ago
The exact error message should be given.
– ederag
2 days ago
@ederag It's pretty obvious what the exact error message would be in case of trying to increment a constant variable. OP asks for the difference of incrementing and creation of temporary values like
n+1
.– πάντα ῥεῖ
2 days ago
@ederag It's pretty obvious what the exact error message would be in case of trying to increment a constant variable. OP asks for the difference of incrementing and creation of temporary values like
n+1
.– πάντα ῥεῖ
2 days ago
@πάνταῥεῖ Sure. Just recalling good practices for a new user. Having the exact message makes the question easily reachable by searching for the error message. Remember that SO is not a forum. Good questions are useful not only to the original poster.
– ederag
2 days ago
@πάνταῥεῖ Sure. Just recalling good practices for a new user. Having the exact message makes the question easily reachable by searching for the error message. Remember that SO is not a forum. Good questions are useful not only to the original poster.
– ederag
2 days ago
@ederag Satisfied now?
– πάντα ῥεῖ
2 days ago
@ederag Satisfied now?
– πάντα ῥεῖ
2 days ago
|
show 3 more comments
2 Answers
2
active
oldest
votes
up vote
2
down vote
Is the second case different from the first case?
Yes they are fundamentally different.
int const n=100;
n++;
The increment operator obviously cannot applied for a const
(ant) variable, because the const
keyword prevents it to be changed after the initial definition. That's why the compiler error is issued.
In the other case, the variable itself isn't changed, but another temporary value is created when it's passed to the operator<<()
of std::cout
.
add a comment |
up vote
1
down vote
In the first case the compiler is asked to compute the output of adding a constant to an integer. This causes no error.
In the second case, the compiler is asked to change the value of a constant. This is illegal and results in a compiler error.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Is the second case different from the first case?
Yes they are fundamentally different.
int const n=100;
n++;
The increment operator obviously cannot applied for a const
(ant) variable, because the const
keyword prevents it to be changed after the initial definition. That's why the compiler error is issued.
In the other case, the variable itself isn't changed, but another temporary value is created when it's passed to the operator<<()
of std::cout
.
add a comment |
up vote
2
down vote
Is the second case different from the first case?
Yes they are fundamentally different.
int const n=100;
n++;
The increment operator obviously cannot applied for a const
(ant) variable, because the const
keyword prevents it to be changed after the initial definition. That's why the compiler error is issued.
In the other case, the variable itself isn't changed, but another temporary value is created when it's passed to the operator<<()
of std::cout
.
add a comment |
up vote
2
down vote
up vote
2
down vote
Is the second case different from the first case?
Yes they are fundamentally different.
int const n=100;
n++;
The increment operator obviously cannot applied for a const
(ant) variable, because the const
keyword prevents it to be changed after the initial definition. That's why the compiler error is issued.
In the other case, the variable itself isn't changed, but another temporary value is created when it's passed to the operator<<()
of std::cout
.
Is the second case different from the first case?
Yes they are fundamentally different.
int const n=100;
n++;
The increment operator obviously cannot applied for a const
(ant) variable, because the const
keyword prevents it to be changed after the initial definition. That's why the compiler error is issued.
In the other case, the variable itself isn't changed, but another temporary value is created when it's passed to the operator<<()
of std::cout
.
edited 2 days ago
answered 2 days ago
πάντα ῥεῖ
71.1k970132
71.1k970132
add a comment |
add a comment |
up vote
1
down vote
In the first case the compiler is asked to compute the output of adding a constant to an integer. This causes no error.
In the second case, the compiler is asked to change the value of a constant. This is illegal and results in a compiler error.
add a comment |
up vote
1
down vote
In the first case the compiler is asked to compute the output of adding a constant to an integer. This causes no error.
In the second case, the compiler is asked to change the value of a constant. This is illegal and results in a compiler error.
add a comment |
up vote
1
down vote
up vote
1
down vote
In the first case the compiler is asked to compute the output of adding a constant to an integer. This causes no error.
In the second case, the compiler is asked to change the value of a constant. This is illegal and results in a compiler error.
In the first case the compiler is asked to compute the output of adding a constant to an integer. This causes no error.
In the second case, the compiler is asked to change the value of a constant. This is illegal and results in a compiler error.
answered 2 days ago
John Murray
591414
591414
add a comment |
add a comment |
Mudit Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Mudit Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Mudit Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Mudit Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239287%2fwhy-do-i-get-a-compiler-error-when-applying-the-increment-operator-to-a-constan%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
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
1
Why did you declare it constant if you were planning to change it?
– Ayxan
2 days ago
The exact error message should be given.
– ederag
2 days ago
@ederag It's pretty obvious what the exact error message would be in case of trying to increment a constant variable. OP asks for the difference of incrementing and creation of temporary values like
n+1
.– πάντα ῥεῖ
2 days ago
@πάνταῥεῖ Sure. Just recalling good practices for a new user. Having the exact message makes the question easily reachable by searching for the error message. Remember that SO is not a forum. Good questions are useful not only to the original poster.
– ederag
2 days ago
@ederag Satisfied now?
– πάντα ῥεῖ
2 days ago