#include<iostream>
class ArrayNotFound
{
public:
static int a;
static int b;
};
int a=10;
int
ArrayNotFound::a=25;
int
ArrayNotFound::b=a;
int main()
{
std::cout<<"ArrayNotFound::b :
"<<ArrayNotFound::b;
return 0;
}
OUTPUT:
ArrayNotFound::b : 25
EXPLANATION:
When a class member is defined outside the class and if the intializer
variable which is used for initializing the class member is present in more than
one scope, then the class member will be initialized with the variable which belongs
to the own class member scope.
I think it is bit confusing, Let me explain with the following
example,
Consider the following statement,
int
ArrayNotFound::b=a;
When you look in to this, class member ‘b’ is defined outside
the class and it is initialized with variable ‘a’. Here a is present in two scopes,
a.
Global scope
int a=10;
b.
Class scope
static int a;
So compiler will get confuse now, and it will follow as per the
standard rule.
It means it will pick up the class scope a instead of global variable
a, because static variable ‘b’ belongs to the ArrayNotFound scope.
No comments:
Post a Comment