typedef vs variable declaration inside a class

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

  1. , India

    , India Guest

    Consider the program

    #include <iostream>

    using namespace std;

    class Test
    {
    public:
    Test(Test_int c_value)
    {
    value = c_value;
    }

    typedef int Test_int;

    private:
    Test_int value;
    };

    int main()
    {
    Test t(100);

    return 0;
    }

    Here I am getting compilation error for the line
    Test(Test_int c_value)
    because it uses Test_int type which is not known at that point and is
    declared afterwards.

    If I declare
    typedef int Test_int;
    before the the ctor, the compilation error goes. But the data member
    'value' is used inside the ctor at which point the definition of the
    variable is not known(it is defined later in the private section).

    My doubt is why the compiler is unable to see the typedef declaration
    for Type_int when it occurs later in the class; but it is able to see
    the data member 'value' used in the ctor but which is actually defined
    inside the private section later in the class.

    Why are the typedef and variable definition inside the class treated
    differently ?

    Kindly explain

    Thanks
    V.Subramanian
     
    , India, Jun 26, 2007
    #1
    1. Advertising

  2. , India wrote:
    > Consider the program
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > class Test
    > {
    > public:
    > Test(Test_int c_value)
    > {
    > value = c_value;
    > }
    >
    > typedef int Test_int;
    >
    > private:
    > Test_int value;
    > };
    >
    > int main()
    > {
    > Test t(100);
    >
    > return 0;
    > }
    >
    > Here I am getting compilation error for the line
    > Test(Test_int c_value)
    > because it uses Test_int type which is not known at that point and is
    > declared afterwards.
    >
    > If I declare
    > typedef int Test_int;
    > before the the ctor, the compilation error goes. But the data member
    > 'value' is used inside the ctor at which point the definition of the
    > variable is not known(it is defined later in the private section).
    >
    > My doubt is why the compiler is unable to see the typedef declaration
    > for Type_int when it occurs later in the class; but it is able to see
    > the data member 'value' used in the ctor but which is actually defined
    > inside the private section later in the class.
    >
    > Why are the typedef and variable definition inside the class treated
    > differently ?
    >
    > Kindly explain
    >
    > Thanks
    > V.Subramanian
    >


    My understanding is that this rule exists to make life easier for
    compiler writers. The C/C++ syntax is ambiguous (in the LALR(1) sense)
    unless you know which names are typedefs and which names are just
    ordinary identifiers. So special requirements are made of typedefs,
    namely that you must declare them before you use them.

    I read all this a long time ago, so don't take my word for it. Someone
    else might have a better explanation.

    john
     
    John Harrison, Jun 26, 2007
    #2
    1. Advertising

  3. , India

    ketan Guest

    Hi Subramanian,

    Typedef is an alias for any valid type. It occupies the same namespace
    as other identifiers and obeys the usual scope rules.

    Applying scope rules once you declare typedef before ctor, you are
    allowed to use typedef till the end of the class scope.
    Hence it will work. But when you declare it after C'tor compile has
    not still seen the typedef and hence does not know
    the alias that you intend to use.

    Hope this explains.
    Ketan

    On Jun 25, 9:49 pm, ", India"
    <> wrote:
    > Consider the program
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > class Test
    > {
    > public:
    > Test(Test_int c_value)
    > {
    > value = c_value;
    > }
    >
    > typedef int Test_int;
    >
    > private:
    > Test_int value;
    >
    > };
    >
    > int main()
    > {
    > Test t(100);
    >
    > return 0;
    >
    > }
    >
    > Here I am getting compilation error for the line
    > Test(Test_int c_value)
    > because it uses Test_int type which is not known at that point and is
    > declared afterwards.
    >
    > If I declare
    > typedef int Test_int;
    > before the the ctor, the compilation error goes. But the data member
    > 'value' is used inside the ctor at which point the definition of the
    > variable is not known(it is defined later in the private section).
    >
    > My doubt is why the compiler is unable to see the typedef declaration
    > for Type_int when it occurs later in the class; but it is able to see
    > the data member 'value' used in the ctor but which is actually defined
    > inside the private section later in the class.
    >
    > Why are the typedef and variable definition inside the class treated
    > differently ?
    >
    > Kindly explain
    >
    > Thanks
    > V.Subramanian
     
    ketan, Jun 26, 2007
    #3
  4. , India

    James Kanze Guest

    On Jun 26, 4:49 am, ", India"
    <> wrote:
    > Consider the program


    > #include <iostream>


    > using namespace std;


    > class Test
    > {
    > public:
    > Test(Test_int c_value)
    > {
    > value = c_value;
    > }


    > typedef int Test_int;


    > private:
    > Test_int value;
    > };


    > int main()
    > {
    > Test t(100);
    >
    > return 0;
    > }


    > Here I am getting compilation error for the line
    > Test(Test_int c_value)
    > because it uses Test_int type which is not known at that point and is
    > declared afterwards.


    Correct.

    > If I declare
    > typedef int Test_int;
    > before the the ctor, the compilation error goes. But the data member
    > 'value' is used inside the ctor at which point the definition of the
    > variable is not known(it is defined later in the private section).


    Correct.

    > My doubt is why the compiler is unable to see the typedef declaration
    > for Type_int when it occurs later in the class; but it is able to see
    > the data member 'value' used in the ctor but which is actually defined
    > inside the private section later in the class.


    > Why are the typedef and variable definition inside the class treated
    > differently ?


    They aren't, but the function body is treated differently from
    the declaration. Informally, everything within the {...} of the
    function is parsed and compiled as if it followed the class
    definition. The part of the declaration which precedes the
    opening brace isn't, however. (In the case of a constructor,
    the initializer list is also processes as if it followed the
    complete class definition.)

    --
    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, Jun 26, 2007
    #4
  5. , India

    James Kanze Guest

    On Jun 26, 8:08 am, John Harrison <> wrote:
    > , India wrote:
    > > Consider the program


    > > #include <iostream>


    > > using namespace std;


    > > class Test
    > > {
    > > public:
    > > Test(Test_int c_value)
    > > {
    > > value = c_value;
    > > }


    > > typedef int Test_int;


    > > private:
    > > Test_int value;
    > > };


    > > int main()
    > > {
    > > Test t(100);


    > > return 0;
    > > }


    > > Here I am getting compilation error for the line
    > > Test(Test_int c_value)
    > > because it uses Test_int type which is not known at that point and is
    > > declared afterwards.


    > > If I declare
    > > typedef int Test_int;
    > > before the the ctor, the compilation error goes. But the data member
    > > 'value' is used inside the ctor at which point the definition of the
    > > variable is not known(it is defined later in the private section).


    > > My doubt is why the compiler is unable to see the typedef declaration
    > > for Type_int when it occurs later in the class; but it is able to see
    > > the data member 'value' used in the ctor but which is actually defined
    > > inside the private section later in the class.


    > > Why are the typedef and variable definition inside the class treated
    > > differently ?


    > My understanding is that this rule exists to make life easier for
    > compiler writers. The C/C++ syntax is ambiguous (in the LALR(1) sense)
    > unless you know which names are typedefs and which names are just
    > ordinary identifiers. So special requirements are made of typedefs,
    > namely that you must declare them before you use them.


    The rule you're thinking of applies to templates, and requires
    the use of typename in some cases. Here, the rule has nothing
    to do with whether the symbol is a type. Within function
    bodies, default arguments, exception-specifications, and
    constructor ctor-initializers (including such things in nested
    classes), the class is considered complete; it's as if these
    things were parsed after the closing brace of the class
    definition. So using Test_int or value within the function
    definition is legal, but using it before the opening brace of
    the function (except as a default argument or in an exception
    specifier) isn't.

    --
    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, Jun 26, 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. Ovidesvideo
    Replies:
    4
    Views:
    515
    Andrey Tarasevich
    Dec 10, 2004
  2. kelvSYC
    Replies:
    6
    Views:
    7,234
    Richard Herring
    May 17, 2005
  3. Bolin
    Replies:
    4
    Views:
    411
  4. oor
    Replies:
    0
    Views:
    1,356
  5. Mehmet Yavuz S. Soyturk

    variable declaration inside eval inside with

    Mehmet Yavuz S. Soyturk, Apr 11, 2007, in forum: Javascript
    Replies:
    3
    Views:
    126
    Mehmet Yavuz S. Soyturk
    Apr 12, 2007
Loading...

Share This Page