By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

use of local class

P: n/a
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

Nov 2 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Nov 2 '05 #2

P: n/a

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.

Nov 2 '05 #3

P: n/a

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

Nov 2 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.