typedef and data member declaration

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Nov 26, 2007.

  1. , India

    , India Guest

    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
     
    , India, Nov 26, 2007
    #1
    1. Advertising

  2. , India

    Jim Langston Guest

    <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Nov 26, 2007
    #2
    1. Advertising

  3. , India

    James Kanze Guest

    On Nov 26, 8:21 am, ", India"
    <> 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:
    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
     
    James Kanze, Nov 26, 2007
    #3
  4. , India

    terminator Guest

    On Nov 26, 12:47 pm, James Kanze <> wrote:
    > On Nov 26, 8:21 am, ", India"
    >
    >
    >
    >
    >
    > <> 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.
     
    terminator, Nov 26, 2007
    #4
  5. , India

    James Kanze Guest

    On Nov 26, 12:46 pm, terminator <> wrote:
    > On Nov 26, 12:47 pm, James Kanze <> 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:
    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
     
    James Kanze, Nov 27, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vu Pham
    Replies:
    16
    Views:
    567
    Nick Hounsome
    Jan 14, 2004
  2. Vu Pham

    typedef and declaration of function

    Vu Pham, Jan 14, 2004, in forum: C Programming
    Replies:
    17
    Views:
    603
    Daniel Haude
    Jan 15, 2004
  3. Jason
    Replies:
    2
    Views:
    539
    Jonathan Mcdougall
    May 13, 2006
  4. oor
    Replies:
    0
    Views:
    1,398
  5. Skybuck Flying
    Replies:
    2
    Views:
    364
    Alf P. Steinbach /Usenet
    Jun 21, 2011
Loading...

Share This Page