#include <iostream> void modify(const int& num) { num = 10; } int main() { int a = 5; modify(a); std::cout << "a: " << a << std::endl; return 0; } (2 Points) No issue b) Incorrect reference type c) Modifying const variable d) Missing const in function prototype
Question
#include <iostream>
void modify(const int& num) {
num = 10;
}
int main() {
int a = 5;
modify(a);
std::cout << "a: " << a << std::endl;
return 0;
} (2 Points)
No issue
b) Incorrect reference type
c) Modifying const variable
d) Missing const in function prototype
Solution
The issue with the code is option c) Modifying const variable. In C++, when you declare a variable as const, you are telling the compiler that the variable is not going to change during the program execution. In the function modify(), the parameter num is passed as a const reference, which means it cannot be modified. However, the function tries to change the value of num, which is not allowed. Therefore, the code will result in a compilation error.
Similar Questions
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.