0% found this document useful (0 votes)
169 views8 pages

Local Classes in C++

A local class in C++ is a class declared inside a function. It is only accessible within that function. Methods of a local class must be defined inside the class. A local class can have static functions but not static data members. Member methods can only access static variables and enums of the enclosing function.

Uploaded by

Abhishek Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
169 views8 pages

Local Classes in C++

A local class in C++ is a class declared inside a function. It is only accessible within that function. Methods of a local class must be defined inside the class. A local class can have static functions but not static data members. Member methods can only access static variables and enums of the enclosing function.

Uploaded by

Abhishek Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

6/21/22, 9:17 PM Local Classes in C++ - GeeksforGeeks

Local Classes in C++


Difficulty Level :
Medium ● Last Updated :
16 Nov, 2021

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.

The methods of a local class must be defined inside it only.

A local class can have static functions but, not static data members.

For example, in the following program, Test is a local class in fun(). 

CPP

// C++ program without any compilation error


// to demonstrate a Local Class
#include <iostream>
using namespace std;
  
// Creating the class
void fun()
{
    // local to fun
    class Test {
        // members of Test class
    };
}
  
// Driver Code
int main() { return 0; }

Following are some interesting facts about Local Classes in C++:

https://www.geeksforgeeks.org/local-class-in-cpp/ 1/11
6/21/22, 9:17 PM Local Classes in C++ - GeeksforGeeks

Start Your Coding Journey Now!


1) A local class type name can only be used in the enclosing function.

Login Register
For example, in the following program, declarations of t and tp are valid in fun(), but

invalid in main(). 

CPP

// A program without any compilation error to demonstrate


// that a local class type name can only be used
// in the enclosing function
  
#include <iostream>
using namespace std;
  
void fun()
{
    // Local class
    class Test {
        // Body
    };
  
    Test t; // Fine
    Test* tp; // Fine
}
  
int main()
{
    Test t; // Error
    Test* tp; // Error
    return 0;
}

2) All the methods of Local classes must be defined inside the class only. For

example, program 1 works fine and program 2 fails in the compilation.

Program 1:

CPP

// C++ program without any compilation error to demonstrate


// that all the methods of Local classes must be defined
// inside the class only
#include <iostream>
using namespace std;
  
void fun()
{

    class Test // local to fun
https://www.geeksforgeeks.org/local-class-in-cpp/ 2/11
6/21/22, 9:17 PM Local Classes in C++ - GeeksforGeeks

    {
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

Local Class method() called

Program 2:

CPP

// C++ program with compilation error to demonstrate that


// all the methods of Local classes must be defined inside
// the class only
#include <iostream>
using namespace std;
  
void fun()
{
    class Test // local to fun
    {
    public:
        void method();
    };
  
    // Error as the method is defined outside the local
    // class
    void Test::method() { cout << "Local Class method()"; }
}
  
int main() { return 0; }

https://www.geeksforgeeks.org/local-class-in-cpp/ 3/11
6/21/22, 9:17 PM Local Classes in C++ - GeeksforGeeks

Start Your Coding Journey Now!


Output

Login Register
 

Compiler Error:

In function 'void fun()':

error: a function-definition is not allowed here before '{' token

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

// A program with compilation error to demonstrate that


// a Local class cannot contain static data members
#include <iostream>
using namespace std;
  
void fun()
{
    class Test // local to fun
    {
        static int i;
    };
}
  
int main() { return 0; }

Output

Compiler Error:

In function 'void fun()':

error: local class 'class fun()::Test' shall not have static data member

Program 2:

CPP

// C++ program without any compilation error to demonstrate


// that a Local class cannot contain static data members
#include <iostream>
using namespace std; ▲

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

Local Class method() called

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 2 fails in the compilation.

Program 1:

CPP

// C++ program without any compilation error to demonstrate


// that member methods of local class can only access static
// and enum variables of the enclosing function
#include <iostream>
using namespace std;
  
void fun()
{
    static int x;
    enum { i = 1, j = 2 };
  
    // Local class
    class Test {
    public: ▲

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

// C++ program with compilation error to demonstrate that


// member methods of local class can only access static
// and enum variables of the enclosing function
#include <iostream>
using namespace std;
  
void fun()
{
    int x;
  
    // Local class
    class Test {
    public:
        void method() { cout << "x = " << x << endl; }
    };
  
    Test t;
    t.method();
}
  
int main() ▲

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:

prog.cpp: In member function ‘void fun()::Test::method()’:

prog.cpp:14:43: error: use of local variable with automatic storage from

containing function

         void method() { cout << “x = ” << x << endl; }

                                           ^

prog.cpp:9:9: note: ‘int x ’ declared here

     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

following program works fine.

CPP

// C++ program without any compilation error to demonstrate


// that Local classes can access global types, variables and
// functions
#include <iostream>
using namespace std;
  
int x;
  
void fun()
{
  
    // First Local class
    class Test1 {
    public:
        Test1() { cout << "Test1::Test1()" << endl; }

    };
https://www.geeksforgeeks.org/local-class-in-cpp/ 7/11
6/21/22, 9:17 PM Local Classes in C++ - GeeksforGeeks

  
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

Must Read: Nested Classes in C++

Like 32

Previous Next

https://www.geeksforgeeks.org/local-class-in-cpp/ 8/11

You might also like