Local class - Functions, Features - C++ basic concepts

Q.  Which of the followings are false statements about Local class?

1. A local class type name can only be used in the enclosing function
2. All the methods of Local classes must be defined inside the class only
3. A Local class can contain static data members.
4. A Local class may contain static functions.
5. Non-static variables of the enclosing function are not accessible inside local classes.
6. Local classes cannot access global types, variables and functions.

- Published on 17 Jul 15

a. Only 1,3
b. Only 3, 6
c. Only 2 , 4 , 6
d. None of these

ANSWER: Only 3, 6
 

    Discussion

  • Nihal   -Posted on 08 Oct 15
    The given below two statements are false about local class.
    • A Local class can contain static data members.
    • Local classes cannot access global types, variables and functions.
    Important points about local class.
    • Local class is a class defined inside a function.
    • A local class type name can only be used in the enclosing function.
    • A Local class cannot contain static data members.
    • Local classes can access global types, variables and functions.
    Example:
    void fun()
    {
    class LocalClass // local class to function fun
    {
    public:

    void method() {
    cout << "Local Class method() is called";
    }
    };

    LocalClass obj;
    obj.method();
    }

    int main()
    {
    fun();
    return 0;
    }

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)