Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typedef and data member declaration

Reply
Thread Tools

typedef and data member declaration

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      11-26-2007
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
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      11-26-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      11-26-2007
On Nov 26, 8:21 am, "(E-Mail Removed), India"
<(E-Mail Removed)> wrote:
> 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:(E-Mail Removed)
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
 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      11-26-2007
On Nov 26, 12:47 pm, James Kanze <(E-Mail Removed)> wrote:
> On Nov 26, 8:21 am, "(E-Mail Removed), India"
>
>
>
>
>
> <(E-Mail Removed)> wrote:
> > 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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-27-2007
On Nov 26, 12:46 pm, terminator <(E-Mail Removed)> wrote:
> On Nov 26, 12:47 pm, James Kanze <(E-Mail Removed)> wrote:


[...]
> > 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class declaration: TMember Member(); vs TMember Member; Skybuck Flying C++ 2 06-21-2011 02:36 PM
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
typedef and declaration of function Vu Pham C++ 16 01-14-2004 10:17 PM



Advertisments