use of local class

N

Nan Li

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
 
V

Victor Bazarov

Nan said:
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
 
N

Nan Li

Victor said:
Nan said:
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.
You can avoid carpal tunnel syndrome by typing less:

struct A { int i; };


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.


Thanks.
 
W

werasm

Nan said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top