Local Classes in C++
Local Classes in C++
A class declared inside a function becomes local to that function and is called Local
Class in C++.
A local class name can only be used locally i.e., inside the function and not outside
it.
A local class can have static functions but, not static data members.
CPP
https://www.geeksforgeeks.org/local-class-in-cpp/ 1/11
6/21/22, 9:17 PM Local Classes in C++ - GeeksforGeeks
Login Register
For example, in the following program, declarations of t and tp are valid in fun(), but
invalid in main().
CPP
2) All the methods of Local classes must be defined inside the class only. For
Program 1:
CPP
{
Start Your Coding Journey Now!
public:
// Fine as the method is defined
Login Register
// inside the local class
void method()
{
cout << "Local Class method() called";
}
};
Test t;
t.method();
}
int main()
{
fun();
return 0;
}
Output
Program 2:
CPP
https://www.geeksforgeeks.org/local-class-in-cpp/ 3/11
6/21/22, 9:17 PM Local Classes in C++ - GeeksforGeeks
Login Register
Compiler Error:
3) A Local class cannot contain static data members. It may contain static functions
though. For example, program 1 fails in compilation, but program 2 works fine.
Program 1:
CPP
Output
Compiler Error:
error: local class 'class fun()::Test' shall not have static data member
Program 2:
CPP
https://www.geeksforgeeks.org/local-class-in-cpp/ 4/11
6/21/22, 9:17 PM Local Classes in C++ - GeeksforGeeks
Start Your Coding Journey Now!
void fun()
{
Login Register
class Test // local to fun
{
public:
static void method()
{
cout << "Local Class method() called";
}
};
Test::method();
}
int main()
{
fun();
return 0;
}
Output
4) Member methods of the local class can only access static and enum variables of
the enclosing function. Non-static variables of the enclosing function are not
accessible inside local classes. For example, program 1 compiles and runs fine. But,
Program 1:
CPP
https://www.geeksforgeeks.org/local-class-in-cpp/ 5/11
6/21/22, 9:17 PM Local Classes in C++ - GeeksforGeeks
void method()
Start Your Coding Journey Now!
{
cout << "x = " << x
Login Register
<< endl; // fine as x is static
cout << "i = " << i
<< endl; // fine as i is enum
}
};
Test t;
t.method();
}
int main()
{
fun();
return 0;
}
Output
x = 0
i = 1
Program 2:
CPP
https://www.geeksforgeeks.org/local-class-in-cpp/ 6/11
6/21/22, 9:17 PM Local Classes in C++ - GeeksforGeeks
{
Start Your Coding Journey Now!
fun();
return 0;
Login Register
}
Error:
containing function
^
int x;
^
5) Local classes can access global types, variables, and functions. Also, local
classes can access other local classes of the same function. For example, the
CPP
Start Your Coding Journey Now!
// Second Local class
class Test2 {
Login Register
// Fine: A local class can use other local classes
// of same function
Test1 t1;
public:
void method()
{
// Fine: Local class member methods can access
// global variables.
cout << "x = " << x << endl;
}
};
Test2 t;
t.method();
}
int main()
{
fun();
return 0;
}
Output
Test1::Test1()
x = 0
Like 32
Previous Next
▲
https://www.geeksforgeeks.org/local-class-in-cpp/ 8/11