473,326 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

typedef and data member declaration

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
4 2004
<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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
5
by: Cancerbero | last post by:
Hi (first, excuse me for my bad english) As I know, the semantics for typedef is: typedef A B; I think this makes B a synonym of A, where A is an existing data type. Is that right? Based...
8
by: Ronny Mandal | last post by:
Consider these two: Typedef struct { int foo, bar } FooBar; struct FooBar { int foo, bar }; What woul be the only difference here; just that I can create new instances by 'Foobar fb, *pfb',...
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
3
by: aaragon | last post by:
Hello everyone, I'm trying to run some simple code but for some reason it doesn't work and I've been staring at it for a long time without a single clue of what's going on. This is what happens,...
4
by: subramanian100in | last post by:
Consider the program #include <iostream> using namespace std; class Test { public: Test(Test_int c_value)
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.