473,387 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

use of local class

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
3 3316
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tim Hill | last post by:
I'm just getting up to speed on Java, and have a question about local class behavior. Here's a mini-app to illustrate: interface Foo { void print(); } class LocalClassTest { public static...
0
by: Steffen | last post by:
Hi! I'm trying to access a EntityBean from a servlet via the bean's local home interface. The EJB and the Servlet are together in one .ear file and I'm using JBoss 3.2.3. I think the...
10
by: Matthias | last post by:
Hello, I thought one major advantage of using functors as e.g. sorting predicates over functions would be that I can do something like this: void foo() { class Predicate { public:
9
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
12
by: PGP | last post by:
I notice that local classes are not supported in C#. Could you please discuss why this is so? Was this a bad coding practice to begin with?
11
by: cj | last post by:
Perhaps someone knows how to do this. When I open a new ASP.NET web service application in VS 2008, it opens with a simple Hello World web service already written. I want to see this work. ...
8
by: Rahul | last post by:
Hi, I have the following code and i get a compilation error, int main() { class Locale { public: static int c;
28
by: cpluslearn | last post by:
Hi, I have a local class inside a function template. I am able to wrap any type in my local class. Is it legal C++? What type of class is Local? Is it a class template or regular class? Thanks...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.