On Nov 26, 8:21 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Consider the following:
Class Test
{
public:
Test(Test_int arg) : val(arg) { }
typedef int Test_int;
private:
Test_int val;
};
This has an error viz, the typedef for Test_int appears
after its usage. But the compiler won't give any error
for using the data member 'val' in the ctor initializer
when the declaration of 'val' data member appears
later.
Both the typedef declaration and data member
declaration appear inside the class only.
My question is: why does the compiler give an error
for using a typedef name when its declaration appears
later whereas the compiler doesn't give an error when a
data member is used when its declaration appears
later ?
It has nothing to do with typedef or not. It depends on where
you use it. For all intents and purposes, function bodies
within a class definition are compiled as if they were outside
of an immediately following the class definition. So within
such function bodies, you can use anything declared in the
class. The function declarations, on the other hand, are parsed
immediately, so any declaration they use must be visible at the
point it is used. Thus:
class Test
{
public:
Test( LocalType ) ; // illegal
void f() {
LocalType t ; // legal
}
int const i = j ; // illegal
void g()
{
int const k = j ; // legal
}
typedef int LocalType ;
int const k = 43 ;
} ;
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34