Open In App

Anonymous Objects in C++

Last Updated : 02 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Class and Objects in C++

An Object is an instance of a Class. A class has no memory allocated it is just like a blueprint, but when it is instantiated (i.e. an object is created) memory is allocated just like the real-world instance of that blueprint.

An object that does not have a reference variable is known as an anonymous object. These are the type of objects created for instance use only.

Properties of Anonymous Objects:

  • We can create an object without a name. Such types of nameless objects are called anonymous objects.
  • The primary purpose of the anonymous object is just for instant use (for one-time usage).
  • An Anonymous object can be passed as an argument to a function.
  • These are used to reduce memory consumption.

Syntax:

 Classname({parameters});

Parameters depend on class members.

Example:

C++




// C++ program to illustrate the
// concept of anonymous object
#include <iostream>
using namespace std;
  
class GFG {
    // Private datamembers
    int a, b;
  
public:
    // Constructor
    GFG() { cout << "Default Constructor Executed\n"; }
  
    GFG(int a1, int b1)
    {
        cout << "Parameterised Constructor Executed\n";
        a = a1;
        b = b1;
    }
  
    // Destructor
    ~GFG() { cout << "Destructor Executed\n"; }
};
  
// Driver Function
int main()
{
    // Creation of  Anonymous Object
    GFG({ 2, 3 });
    // Creation of  Anonymous Object
    GFG({ 4, 5 });
    return 0;
}


Output

Parameterised Constructor Executed
Destructor Executed
Parameterised Constructor Executed
Destructor Executed

In the above example, the anonymous object is created, initializes the data members, and gets destroyed. 

Calling Member Functions with the Help of an Anonymous Object

Calling member functions with the help to call the function, we require an object, but after implementing the function, we are not using that object anymore. Hence for this one-time requirement anonymous object is the best choice. It saves time for creating objects and also saves memory.

Syntax:

Classname({parameters}). function name({parameters});

Example:

C++




// C++ program to illustrate the
// concept of anonymous object
#include <iostream>
using namespace std;
  
class GFG {
    // Private datamembers
    int a, b;
  
public:
    // Default constructor
    GFG() {}
  
    // Parameterised Constructor
    GFG(int a1, int b1)
    {
        cout << "Constructor Executed\n";
        a = a1;
        b = b1;
    }
  
    // Member function
    void display()
    {
        cout << a << "\t" << b;
        cout << endl;
    }
  
    ~GFG() { cout << "Destructor Executed\n"; }
};
  
// Driver Function
int main()
{
    // Invoking Member functions
    // with the help of
    // Anonymous Object
    GFG({ 2, 3 }).display();
    GFG({ 4, 5 }).display();
    return 0;
}


Output

Constructor Executed
2    3
Destructor Executed
Constructor Executed
4    5
Destructor Executed

Usage of Anonymous Objects in Operator Overloading

When we have created two anonymous objects. These objects don’t contribute much their only function is to perform the operation and transfer the result into a normal object. If we want to refer to the objects after the implementation of the statement, then in such cases we can create the normal object but, when we are not referring to the object anymore in that case it is better to use anonymous objects.

Example:

C++




// C++ program to illustrate the
// concept of anonymous object
#include <iostream>
using namespace std;
  
// Private datamembers
class Complex {
    int real, img;
  
public:
    // Default constructor
    Complex() {}
  
    // Parameterised Constructor
    Complex(int r, int i)
    {
        real = r;
        img = i;
    }
  
    // getter
    int getReal() { return real; }
  
    // getter
    int getImg() { return img; }
  
    // Operator overloading function
    Complex operator+(Complex c)
    {
        Complex temp;
        temp.real = this->real + c.real;
        temp.img = this->img + c.img;
        return temp;
    }
};
  
// Driver Function
int main()
{
    Complex c3;
    c3 = Complex({ 2, 3 }) + Complex({ 5, 6 });
  
    cout << c3.getReal() << "+" << c3.getImg() << "i"
         << endl;
  
    return 0;
}


Output

7+9i


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads