68 questions
16
votes
3
answers
913
views
Necessity of `typename` for naming of local nested classes in function templates
I have a function template f, defining in its body a local class A with another nested class B. Both classes are not templates. Must I name the inner class as typename A::B or shorter variant A::B is ...
Advice
2
votes
4
replies
249
views
Can function-local class be defined in the global scope?
Any C++ class can be first forward declared, and defined only later in the program. Are function-local classes an exception from this rule?
Please consider a simplified program:
auto f() {
struct ...
14
votes
1
answer
373
views
Are pointers to pure virtual member functions of local classes allowed?
Consider the following snippet:
int main() {
struct Local {
virtual void foo() = 0;
};
void (Local::* ptr)() = &Local::foo;
}
When compiling with C++20, GCC 13.3.0 and Clang 18.1.3 ...
1
vote
1
answer
119
views
Can we declare interface inside a method?
"You cannot declare an interface inside a block; interfaces are inherently static" This is sentence from https://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html#local-classes-...
0
votes
0
answers
177
views
Can friend function of a local class be defined in namespace scope?
A local class in C++ can have friend functions, but these functions cannot be defined neither inside the class [class.friend] p5:
A function may be defined in a friend declaration of a class if and ...
0
votes
0
answers
96
views
JDK8: No enclosing instance of type Object is accessible. Must qualify the allocation with an enclosing instance of type Person (e.g. x.new A()
Hi,
I am following the Java Tutorial for Lamda Expressions Approach 3 Specify Search Criteria Code in a Local Class at https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#...
3
votes
2
answers
3k
views
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 ...
1
vote
2
answers
234
views
Are all local classes and all anonymous classes also inner classes?
This question is mostly about proper Java terminology.
I am making the distinction between an inner class, which is tied to instance of its enclosing scope, and an non-inner, nested static class which ...
0
votes
0
answers
227
views
Get constructor for local-class in non-static method by reflection
I need Constructor<TestClass> instance for local class TestClass in my JUnit4 test method.
public void testMethod() throws NoSuchMethodException {
class TestClass {
}
...
2
votes
0
answers
55
views
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 ...
0
votes
1
answer
66
views
Does local class of interface exist?
We known that local class of a class exists (for example: local class inside a method of a class), but can we declare a local class of an interface. I reformat the original of local class as follows:
&...
1
vote
1
answer
272
views
Use Clang LibTooling to scan C++ source that has call to local class in a templated parent class
Source code to scan:
template <typename T>
class HB {
T m;
public:
void HBfunc1();
};
template <typename T>
void HB<T>::HBfunc1() {
class HC {
public:
...
1
vote
4
answers
337
views
How to make function friend of local class?
Please read the code to know the problem :
#include <iostream>
void fun(int value)
{
//starts local class definition
class test
{
int x;
public:
test(int a) : x(...
63
votes
4
answers
3k
views
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<T> {
void m() {
class Z {}
for (Object o : Arrays.asList(1, 2, 3))
...
1
vote
2
answers
1k
views
How to reference local class
I have a local class...
public class Outer {
public void wrapper() {
class Local {
}
}
}
and I have a test that needs to reference the local class...
Outer.wrapper.Local....
0
votes
1
answer
119
views
Examples of inner, nested, local and anonymous classes from JDK, JRE
I find a very useful to read this question: Java inner class and static nested class, but can't find there any example from Java. Could you provide me with real examples of using those classes from ...
0
votes
2
answers
132
views
Anonymous member classes in Java
On the subject of Anonymous classes, the Oracle documentation states that...
They are like local classes except that they do not have a name. Use them if you need to use a local class only once
...
1
vote
1
answer
2k
views
Call Local class within PAI Module
I have a program with 4 includes in it. One top-include (global data), one for pai-modules, one for pbo-modules and one for a local helper class.
I put the definition and implementation of my local ...
0
votes
2
answers
154
views
Local classes with Lambda expressions
As I tested, the below code executes without any issues. But I could not understand the logic. Can someone please explain?
public static void main(String[] args) {
List<String> london = new ...
0
votes
0
answers
49
views
Inheritance with local classes [duplicate]
So i was working with local classes this morning when I encountered a strange behaviour of inheritance regarding scopes.
public class Foo{
public void printer(){
class Hello extends Bar{
...
4
votes
3
answers
2k
views
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<iostream>
int main()
{
...
0
votes
1
answer
540
views
How to define a friend function operator>> inside a local class?
Trying to overload input operator >> inside a local class. I tried to define friend istream &operator >> inside class Data.
int readFile(char* file_name,float temperature_data[][31])
{...
2
votes
4
answers
153
views
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'...
2
votes
4
answers
897
views
Why does an inner class instance remian in the memory even if the outer class object is destroyed?
Please consider following two classes:
a.) Student
package datatypes;
public class Student {
private String name ;
public Student(String name) {
this.name = name;
}
class ...
0
votes
0
answers
247
views
Is it a good Practice to create Local Classes?
Most of the time i see that programmers say Local Classes are not good for some reasons and say instead of creating classes in methods, create new java files and use them. My question is, suppose that ...
34
votes
2
answers
2k
views
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 ...
0
votes
1
answer
579
views
Add property to arbitrary java bean
I want to write a function that, given an arbitrary java bean as an argument, returns an object that is a copy of that bean but that belongs to an anonymous subclass of the bean's type that contains ...
-1
votes
1
answer
75
views
Access control modifiers in local classes - why don't they work? [duplicate]
I have a local class declared in a method, whose fields are declared as being private. However, I am still able to access them directly from the body of the enclosing method - why is this?
As a side ...
-1
votes
1
answer
39
views
Can we say that a method-local class is a type of Inner class?
Since we can not use the static modifier with a local class defined inside a method, and since Nonstatic nested classes are Inner classes, we could probably say that a method local class is a type of ...
-3
votes
2
answers
3k
views
What calls the greet() method in HelloWorldAnonymousClasses?
In the HelloWorldAnonymousClasses example program (from here):
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, ...
2
votes
1
answer
100
views
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 ...
2
votes
3
answers
386
views
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 ...
0
votes
2
answers
77
views
Is there something wrong with the local class defined withn a constructor body
I have the following enum:
enum FilterFactory {
INSTANCE;
private final Map<FilterType, Creator> creators;
private FilterFactory() {
creators = new HashMap<>();
...
2
votes
0
answers
25
views
Variable lookup in derived local class of template function [duplicate]
I'm experimenting with local classes in C++ and stuck with following code:
void f1(int a)
{
struct Inner1
{
int a;
};
struct Inner2 : Inner1
{
void foo()
{...
6
votes
1
answer
119
views
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 <iostream>
template<int M, class Pred>
constexpr auto fun(Pred pred)...
8
votes
4
answers
1k
views
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() {}
}
...
2
votes
1
answer
130
views
unable to watch java local class instance in eclipse
Here is a small sample:
public class LocalClassSample {
public static void main(String[] args) {
class Utils {
public void printHello(String name) {
System.out....
1
vote
1
answer
71
views
Inner Local Classes in Java
public class Main {
public static void main(String[] args) {
int b=1;
final int c=2;
String s1[] = new String[]{"A","B","C"};
class InnerMain{ ...
2
votes
2
answers
466
views
Local class instance creation expression in a static-context
The JLS 15.9.2 tells us how to determine an enclosing instance:
Let C be the class being instantiated, and let i be the instance being created.
If C is an inner class, then i may have an ...
6
votes
3
answers
229
views
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<class T>
struct A
{
};
template<class T>
void f(T)
{
struct C
{
};
...
1
vote
1
answer
306
views
Local type as template argument inside not instantiated function
Local type as template argument is forbidden in C++03:
template<typename T>
struct Foo { };
void Make()
{
struct Unknown {};
Foo<Unknown> foo; // Bad
}
Is there any directives in ...
16
votes
1
answer
1k
views
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 ...
2
votes
2
answers
483
views
passing local class function pointer to std::list::sort
I am trying to sort a member variable type std::list using a local function. Since C++ doesn't allow local functions and hence Herb Sutter's suggests local classes, I ended up with the following code. ...
2
votes
5
answers
5k
views
Why can't create instance of local class in Java?
If I have this code.
public class Test{
{
class People {
}
}
public static void main(String[] args) {
People person = new People();//...
1
vote
2
answers
183
views
Does java forward referencing act differently in different situations?
Please take a look at this snippet:
public class A {
void method() {
System.out.print(B.j);//This is legal!
class C {
void method () {
...
3
votes
5
answers
3k
views
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() ...
...
3
votes
1
answer
175
views
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 ...
0
votes
2
answers
475
views
Java: Using a local class in a parameterized type for a local variable referenced inside the local class
I am implementing a certain algorithm in a single method in Java. This algorithm needs a data structure that will not be used anywhere else, so it seems appropriate to me to use a local class. The ...
0
votes
3
answers
755
views
How to use friend function of local class?
Since a friend function can be declared in a local class as shown in the following example. How can it be used to access members of local class when it is defined in the function definition which ...
1
vote
1
answer
102
views
Why does Java complain that it can't find my local class?
I am trying to setup Dozer to perform a complex mapping between my two entities. Essentially, I want it to convert my percentCompleted double to a boolean, based on if the value is 1 (100%) or not.
...