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
5
votes
3 answers
Is a local class dependent if declared within a function template?
Current C++ compilers (latest gcc, clang) require the typename keyword in the example below:
template
struct A
{
};
template
void f(T)
{
struct C
{
};
typedef typename A::Type Type; // typename required
}
If…

willj
- 2,991
- 12
- 24
4
votes
3 answers
What is the reason to we can not define friend function in local class?
I have a following snippet code of c++. Declared a class inside main() function.
What is the reason to we can not define friend function in local class?
#include
int main()
{
class Foo
{
void foo() {} // Ok
friend…

msc
- 33,420
- 29
- 119
- 214
4
votes
2 answers
C++ can local class reference be passed to a function?
I would like to know if the following is allowed:
template < class C >
void function(C&);
void function() {
class {} local;
function(local);
}
thanks

Anycorn
- 50,217
- 42
- 167
- 261
4
votes
4 answers
Is there such a thing as a "local interface" in Java?
Java allows me to define local abstract classes, like in this example:
public class Foo {
public void foo() {
abstract class Bar { // Bar is a local class in foo() ...
abstract void bar();
}
new…

Markus A.
- 12,349
- 8
- 52
- 116
4
votes
6 answers
static member variable inside a local class in c++?
I know we cannot declare a static member variable inside a local class... but the reason for it is not clear.
So please can anybody explain it?
Also, why can't we access a non-static variable defined inside the function, within which the local class…

ishan
- 301
- 2
- 9
3
votes
2 answers
Why is there a ClassLoader exception when there are 2 objects with similar type names
I have a class with 2 methods.
In method1(), I create a local record called Abc. This local record is only available to method1() because it was defined in method1() (here are the rules on it according to Java).
In method2(), I create a local…

davidalayachew
- 1,279
- 1
- 11
- 22
3
votes
4 answers
Access problem in local class
void foobar(){
int local;
static int value;
class access{
void foo(){
local = 5; /* <-- Error here */
value = 10;
}
}bar;
}
void main(){
foobar();
}
Why doesn't access…

Howard
- 31
- 1
3
votes
1 answer
How to use local classes with templates?
GCC doesn't seem to approve of instanciating templates with local classes:
template
void f(T);
void g()
{
struct s {};
f(s()); // error: no matching function for call to 'f(g()::s)'
}
VC doesn't complain.
How should it be…

uj2
- 2,255
- 2
- 21
- 32
3
votes
2 answers
How to get address of member function for local class defined in function (C++)
I am trying to do the following: Obtain the address of a member function from a class that was locally defined within a function.
class ConnectionBase
{
};
template class
class ConnectionImpl : public…

czuger
- 804
- 6
- 7
3
votes
1 answer
Is there a better way implementing Java-like local class in C++?
There are situations that I have to choose local classes over lambda when overloading operator() is not enough or when I need virtual functions or something else.
um.. for example:
I need a object that captures local variables, and holds more than…

BlueWanderer
- 2,671
- 2
- 21
- 36
2
votes
0 answers
Leaking the local class outside function
I have a function named return_local where it returns an instance of a local class enclosed with a lambda.
auto return_local() {
return [] {
static int sample { 5 };
struct Point {
int x, y;
int show_sample() { return sample;…

Desmond Gold
- 1,517
- 1
- 7
- 19
2
votes
3 answers
Java local classes and interfaces
I was wondering if the next thing is possible for implementation:
Lets say I've got 2 interfaces while each one of them has 1 function header.
For example, iterface1 has function g(...) and interface2 has function f(...)
Now, I make a class and…

user550413
- 4,609
- 4
- 25
- 26
2
votes
4 answers
Is there something called Local Static Inner Class?
i was just experimenting with inner classes and came across this idea of having local yet static inner class... well i made an inner class inside a static method.. well it's just simple as that..
Here's the example i did
class Outer {
static…

Dilini Peiris
- 446
- 1
- 6
- 16
2
votes
1 answer
contradicting statement regarding to local class, which one is correct?
I'm new to Java and is trying to learn the concept of local class. I'm currently reading the chapter on local class on the Offical Java Documentation Oracle. I have encountered two statements in this chapter that seem to contradict each other. Could…

Thor
- 9,638
- 15
- 62
- 137
2
votes
3 answers
Why can't a local class that extends an inner class access the inner class enclosing instance?
(I keep re-reading that question title and thinking about how ridiculous it must look, but I assure you that is the best description of the problem, and I have an actual application where this is the best structure. I swear I'm not crazy.)
Consider…

snickers10m
- 1,709
- 12
- 28