By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,081 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,081 IT Pros & Developers. It's quick & easy.

typedef and data member declaration

P: n/a
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
Nov 26 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
<su**************@yahoo.comwrote in message
news:b0**********************************@s12g2000 prg.googlegroups.com...
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.
Nov 26 '07 #2

P: n/a
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
Nov 26 '07 #3

P: n/a
On Nov 26, 12:47 pm, James Kanze <james.ka...@gmail.comwrote:
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 ;
sorry to terminate but wasn`t it j?I mean:
int const j = 43 ;
} ;
best,
FM.
Nov 26 '07 #4

P: n/a
On Nov 26, 12:46 pm, terminator <farid.mehr...@gmail.comwrote:
On Nov 26, 12:47 pm, James Kanze <james.ka...@gmail.comwrote:
[...]
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 ;
Yes. As written, all of the uses of j are illegal.
} ;
--
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
Nov 27 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.