use of local class

Discussion in 'C++' started by Nan Li, Nov 2, 2005.

  1. Nan Li

    Nan Li Guest

    I just discovered you can have a local class defined inside a function
    like this:


    #include <iostream>

    using namespace std;

    int main(int argc, char* argv[])
    {
    class A { public: int i; };
    A a;
    a.i = 5;
    cout << a.i << endl;
    return 0;
    }


    But I was wondering what's the possible use of this. Pleae shed some
    light.


    Thanks,
    Nan
     
    Nan Li, Nov 2, 2005
    #1
    1. Advertising

  2. Nan Li wrote:
    > I just discovered you can have a local class defined inside a function
    > like this:
    >
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > int main(int argc, char* argv[])


    You should avoid declaring unnecessary stuff. Neither 'argc' nor 'argv'
    are used in your program.

    > {
    > class A { public: int i; };


    You can avoid carpal tunnel syndrome by typing less:

    struct A { int i; };

    > A a;
    > a.i = 5;
    > cout << a.i << endl;
    > return 0;
    > }
    >
    >
    > But I was wondering what's the possible use of this. Pleae shed some
    > light.


    Strange question. What's the possible use of local variables? If you
    can answer that question, you can answer your own question about local
    types. The purpose is essentially the same.

    V
     
    Victor Bazarov, Nov 2, 2005
    #2
    1. Advertising

  3. Nan Li

    Nan Li Guest

    Victor Bazarov wrote:
    > Nan Li wrote:
    > > I just discovered you can have a local class defined inside a function
    > > like this:
    > >
    > >
    > > #include <iostream>
    > >
    > > using namespace std;
    > >
    > > int main(int argc, char* argv[])

    >
    > You should avoid declaring unnecessary stuff. Neither 'argc' nor 'argv'
    > are used in your program.
    >

    Yes. I generated the skeleton code automatically at first and didn't
    realize that.

    > > {
    > > class A { public: int i; };

    >
    > You can avoid carpal tunnel syndrome by typing less:
    >
    > struct A { int i; };
    >
    > > A a;
    > > a.i = 5;
    > > cout << a.i << endl;
    > > return 0;
    > > }
    > >
    > >
    > > But I was wondering what's the possible use of this. Pleae shed some
    > > light.

    >
    > Strange question. What's the possible use of local variables? If you
    > can answer that question, you can answer your own question about local
    > types. The purpose is essentially the same.
    >

    Right. I can think of all the benefits of local scope. I also think
    there must be some situation where using a local class becomes handy.
    But I just haven't found one yet. That's why I asked the question.


    > V


    Thanks.
     
    Nan Li, Nov 2, 2005
    #3
  4. Nan Li

    werasm Guest

    Nan Li wrote:

    > Right. I can think of all the benefits of local scope. I also think
    > there must be some situation where using a local class becomes handy.
    > But I just haven't found one yet. That's why I asked the question.


    Oh I have, encapsulation... If no one else needs to know about it, then
    so be it. That's one big one...

    Regards,

    Werner
     
    werasm, Nov 2, 2005
    #4
    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. karim
    Replies:
    1
    Views:
    776
    George Ter-Saakov
    Jun 26, 2003
  2. E11
    Replies:
    1
    Views:
    4,781
    Thomas Weidenfeller
    Oct 12, 2005
  3. Shawn
    Replies:
    4
    Views:
    460
    Furious George
    Oct 13, 2006
  4. QQ
    Replies:
    2
    Views:
    898
    Patricia Shanahan
    May 1, 2008
  5. Max Williams
    Replies:
    0
    Views:
    116
    Max Williams
    Jun 14, 2009
Loading...

Share This Page