0

If the code entered a scope and defined a new object with the same name as the object outside, Will the newly defined object be correctly defined?
As following code shows:

class ClassB
{
    ClassA* p;
    ClassB(ClassA* a)
    {
         p = NULL;
         if (a != NULL)
               p = a;
    }
};

ClassA x;
if (some condition)
{
    ClassB x(x);
    // Will x->p be NULL or it will be pointer a?
}

In the if scope, x-> is NULL here or a?
I can see in gcc, it will be NULL. But in VS2015 it seems a.
So my question is that what does the standard says for this part. Thanks a lot.

1 Answers1

0

Just dont do it. The fact that you have to ask here and that the answer isnt 100% obvious means that anybody else reading to code will also be confused by it. As as a purely academic question, it is interesting to know if it would work, but in any real code simply do this:

ClassA x;
if (some condition)
{
    ClassB y(&x);
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 2
    i think question was rather if second x was initialized like int x{x}; would its value be value of first x or its value would be whatever it was before initialization – Andrew Kashpur Aug 23 '17 at 07:41
  • @AndrewKashpur tried to fix the answer. Now its rather a comment, but i'll just leave it unless it gets too many downvotes... – 463035818_is_not_an_ai Aug 23 '17 at 07:48