When we declare a class inside a function the class becomes local class in c++. The scope of the local classes is the function in which they’re defined. There are some interesting facts about the local classes.

  • All the methods of Local Classes must be defined inside the class only.
  • They cannot define static member variables.
  • They cannot access non-static “automatic” local variables of the enclosing function. But they can access the static variables.
  • They can be used in template functions.
  • If they defined inside template function, then they can use the template parameters of the enclosing function.
  • Local classes are final, that means users outside the function cannot derive from local class to function.

Example code for local class

using namespace std;

int mylocal_func()
          class Myclass {
                  int a;
                  void add(){


int main()
              return 0;

Related Contents to follow