typedef and data member declaration

S

subramanian100in

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 ?

Kindly clarify.

Thanks
V.Subramanian
 
J

Jim Langston

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 ?

Consider:

class Foo
{
public:
Foo( int i ): val( i ) {}
private:
int val;
};

Same situation, in the constructor, val has not been defined yet yet it
doesn't complain. classes seem to be somewhat unique in this in that the
member variables do not have to be declared before their usage.
 
J

James Kanze

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 ;
} ;
 
T

terminator

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 ;
sorry to terminate but wasn`t it j?I mean:
int const j = 43 ;

best,
FM.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top