Issue
#include <iostream>
struct A
{
A(int n) { std::cout << n; }
int n{2};
};
int main()
{
A a{1};
}
The output is 1
rather than 2
.
Does the C++ standard define that the argument name is preferred if it is the same as that of a data member?
Solution
The argument is in a "closer" scope than the member variable, so the argument shadows the member variable.
The obvious solution is to rename the argument (or the member variable), so they are not the same anymore.
You can also use this->n
to explicitly use the member variable.
Answered By - Some programmer dude Answer Checked By - Robin (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.