Following code does compile whereas name “aNumber” is not declared before use.
class A
{
A()
:aNumber(100)
{
}
void foo()
{
aNumber = 0;
}
int aNumber;
};
If above code compiles then why not following :-
A.
class Dummy
{
void foo(INT);
typedef int INT;
};
B.Default initialization by member variable :-
class Dummy
{
void foo(int y = x);
int x;
};
You’re comparing apples and oranges. The following code is OK:
The problem is not what you declare, but where the declarations are used. Method definitions, even it they appear lexically inside a class, are compiled as if they are declared inside the class, but defined outside and after the class declaration, and so are default arguments.