Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method.
Questions tagged [local-class]
64 questions
125
votes
9 answers
Use of class definitions inside a method in Java
Example:
public class TestClass {
public static void main(String[] args) {
TestClass t = new TestClass();
}
private static void testMethod() {
abstract class TestMethod {
int a;
int b;
…

bragboy
- 34,892
- 30
- 114
- 171
63
votes
4 answers
Error "illegal generic type for instanceof" when using local classes
I have the following Java code that uses a local class.
import java.util.Arrays;
public class X {
void m() {
class Z {}
for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {}
}
}
It does not compile…

Lukas Eder
- 211,314
- 129
- 689
- 1,509
55
votes
1 answer
Member template in local class
Given the following code:
void f()
{
class A
{
template
void g() {}
};
}
g++ 4.4 (and also g++-4.6 -std=gnu++0x) complains: "invalid declaration of member template in local class".
Apparently local classes…

HighCommander4
- 50,428
- 24
- 122
- 194
34
votes
2 answers
Why generic lambdas are allowed while nested structs with templated methods aren't?
As far as I understand - generic lambdas are transformed into objects of local scope structs with templated operator(). This makes generic lambda very powerful and easy to use tool. On the other hand one can create structs nested into the function,…

W.F.
- 13,888
- 2
- 34
- 81
33
votes
4 answers
Why aren't static data members allowed in local classes?
What is the reasoning to why static const members cannot exist in local classes? It seems like a rather silly restriction.
Example:
void foo() {
struct bar {
int baz() { return 0; } // allowed
static const int qux = 0; // not allowed?!?
…

Pubby
- 51,882
- 13
- 139
- 180
21
votes
3 answers
Local classes : C++03 vs. C++11
Is there any change in the usage of local class in C++11?
It seems in C++03 local classes cannot be used as template argument (I recall that).
Consider this code,
template void f(const T&) {}
//Note : S is a local class defined inside…

Nawaz
- 353,942
- 115
- 666
- 851
17
votes
6 answers
Why field inside a local class cannot be static?
void foo (int x)
{
struct A { static const int d = 0; }; // error
}
Other than the reference from standard, is there any motivation behind this to disallow static field inside an inner class ?
error: field `foo(int)::A::d' in local class cannot…

iammilind
- 68,093
- 33
- 169
- 336
16
votes
1 answer
Local class can access non-final variable in java 8
Before Java 8, We were not able to use non-final variables inside local class. But now they are allowing final as well as effectively final(who's values has not been changed), can be referred by local classes. What i know(Correct me if i am wrong),…

Ashok
- 487
- 5
- 22
14
votes
2 answers
Why can't a std::vector take a local type?
void foo() {
struct Foo { .. };
std::vector vec; // why is this illegal?
}
I'm not returning Foo to the outside world. It's just a temporary type that I use within the function.

anon
- 41,035
- 53
- 197
- 293
8
votes
4 answers
Retrieving class inside a method using reflection
class test {
public static void main(String[] args) {
new test();
}
void method() {
class inside {
int a;
void methodinside() {}
}
}
}
I was declaring class using reflection…

newbie
- 929
- 17
- 35
7
votes
4 answers
Java. local classes is there any reason not to make it final?
I have a question about local classes in Java (classes that declares in the method or in blocks bounded by { }).
Is there any reason not to declare local class as final? We cannot inherit other class from local class (if it's not defined at the same…

user197284
- 281
- 1
- 3
- 7
7
votes
2 answers
Why can't a struct defined inside a function be used as functor to std::for_each?
The following code won't compile. The compiler complains about *no matching function for call to for_each*. Why is this so?
#include

Oswald
- 31,254
- 3
- 43
- 68
6
votes
3 answers
Are Local class, Inner class and Nested class are the same things in C++?
Are Local class, Inner class and Nested class mean same things in C++?

user366312
- 16,949
- 65
- 235
- 452
6
votes
1 answer
Name lookup for local class members inside templates
Consider the following code, that simulates a constexpr lambda (proposed for C++17, not available in C++14).
#include
template
constexpr auto fun(Pred pred)
{
return pred(1) <= M;
}
template
struct C
{
…

TemplateRex
- 69,038
- 19
- 164
- 304
5
votes
4 answers
Local Classes in C++
I am reading "Local Classes" concept in Object-oriented programming with C++ By Balagurusamy (http://highered.mcgraw-hill.com/sites/0070593620/information_center_view0/).
The last line says "Enclosing function cannot access the private members of a…

Shubhendra Singh
- 51
- 1
- 2