Skip to main content

Anonymous Classes in C++

|

In C++, anonymous classes are classes without a name. 

These classes are supported by C++ and can be useful in specific situations.


Key Characteristics:

- No Constructors: Anonymous classes cannot have constructors.

- Destructors Allowed: They can, however, have destructors.

- Function Limitations: They cannot be passed as arguments to functions or used as return values from functions.


Examples to Illustrate Anonymous Classes

1. Creating a Single Object of an Anonymous Class

In this example, an anonymous class is created with an object named `obj1`. The scope of `obj1` extends throughout the program, allowing access within the main function. In the main function, `obj1` is used to call member functions of the anonymous class.


// CPP program to illustrate the concept of Anonymous Class

#include <iostream>

using namespace std;

// Anonymous Class : Class is not having any name

class {

    // data member

    int i;  

public:

    void setData(int i) {

        // this pointer is used to differentiate

        // between data member and formal argument

        this->i = i;

    }

    void print() {

        cout << "Value for i : " << this->i << endl;

    }

} obj1; // object for anonymous class


// Driver function

int main() {

    obj1.setData(10);

    obj1.print();

    return 0;

}

Output:

Value for i : 10


2. Creating Two Objects of an Anonymous Class

In this example, two objects `obj1` and `obj2` are created for an anonymous class, and the member functions of the class are called. The scope of `obj1` and `obj2` extends throughout the program, allowing multiple objects to be created for the anonymous class.


// CPP program to illustrate the concept of Anonymous Class

#include <iostream>

using namespace std;

// Anonymous Class : Class is not having any name

class {

    // data member

    int i;  

public:

    void setData(int i) {

        // this pointer is used to differentiate

        // between data member and formal argument

        this->i = i;

    }

    void print() {

        cout << "Value for i : " << this->i << endl;

    }

} obj1, obj2; // multiple objects for anonymous class


// Driver function

int main() {

    obj1.setData(10);

    obj1.print();


    obj2.setData(20);

    obj2.print();

    return 0;

}

Output:

Value for i : 10

Value for i : 20


3. Restricting the Scope of an Anonymous Class

To restrict the scope of objects for the anonymous class, the `typedef` keyword can be used. In this example, `typedef` is used to assign a convenient name to the class. Using this name, multiple objects `obj1` and `obj2` are created for the anonymous class. This approach helps in controlling the scope of the objects, which are confined to the main function.


// CPP program to illustrate the concept of Anonymous Class by scope restriction

#include<iostream>

using namespace std;

// Anonymous Class : Class is not having any name

typedef class {

    // data member

    int i;  

public:

    void setData(int i) {

        // this pointer is used to differentiate

        // between data member and formal argument

        this->i = i;

    }

    void print() {

        cout << "Value for i : " << this->i << endl;

    }

} myClass; // using typedef to give a proper name


// Driver function

int main() {

    // multiple objects

    myClass obj1, obj2;

    obj1.setData(10);

    obj1.print();


    obj2.setData(20);

    obj2.print();

    return 0;

}

Output:

Value for i : 10

Value for i : 20


Conclusion

Anonymous classes in C++ can be a valuable tool in specific scenarios where you need a temporary class without the overhead of naming and defining it separately. Understanding the constraints and use cases of anonymous classes can help you write cleaner and more efficient code.


Comments

Popular posts from this blog

Understanding push_back and emplace_back in C++

| Understanding push_back and emplace_back in C++ C++ provides several mechanisms to add elements to its containers, and two often used are push_back and emplace_back . Understanding the difference between these methods can help you write more efficient and expressive code. Let's delve into these concepts with examples to illustrate their usage and benefits.

Reasons for a C++ Program Crash

C++ programs may crash unexpectedly for various reasons. Here are some typical causes of such crashes: Segmentation Fault A segmentation fault is a major cause of program crashes. It occurs when: Attempting to access a memory location that doesn’t exist. Trying to write to a read-only memory location. Accessing protected memory locations, such as kernel memory. Example: int main() {     char *text;     // Stored in the read-only part of the data segment     text = "ABC";     // Problem: trying to modify read-only memory     *(text + 1) = 'n';     return 0; }   Stack Overflow Stack overflow happens due to non-terminating recursion, which exhausts the stack memory. Example: #include <stdio.h> void functionRecursive(int num)  {     if (num =...

Local Classes in C++: A Deep Dive

Understanding Local Classes in C++: A Deep Dive A class declared within a function is known as a local class in C++. These classes are specific to the function in which they are declared and offer several unique characteristics and constraints. What is a Local Class? A local class is a class defined within a function. For instance, in the following example, the Test  class is local to the fun()  function: #include<iostream>  using namespace std;  void fun()    {      class Test  // local to fun      {          // members of the Test class     };  }  int main()  {      return 0;  } Interesting Facts About Local Classes Scope Limitation A local class type name can only be used within the enclosing function. For example, in the following program, the LocalClass is valid within myFunction() , but not in main() . #include <iostream> void myFunct...