Local class in C++: Declaring a class within a function: Examples and Limitations

When a class is declared inside a function then only the function knows about that class.

For Example:

#include <iostream>

using namespace std;

void function();

int main()



return 0;


void f()


class test {

int x;


void put_x(int y) { x=n; }

int get_x() { return x; }

} ob;


cout << ob.get_x();


Limitations of Local class:

1) A local class cannot access local variables .

2) Within the class declaration all member functions must be defined.

3) Inside a local class static variables can’t be declared.

Leave a Reply