PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 20, 2022

[FIXED] What if an argument has the same name as that of a data member?

 October 20, 2022     c++, class, datamember, namespaces, standards     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing