Anonymous Objects in C++
Last Updated :
02 Jan, 2023
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++
#include <iostream>
using namespace std;
class GFG {
int a, b;
public :
GFG() { cout << "Default Constructor Executed\n" ; }
GFG( int a1, int b1)
{
cout << "Parameterised Constructor Executed\n" ;
a = a1;
b = b1;
}
~GFG() { cout << "Destructor Executed\n" ; }
};
int main()
{
GFG({ 2, 3 });
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++
#include <iostream>
using namespace std;
class GFG {
int a, b;
public :
GFG() {}
GFG( int a1, int b1)
{
cout << "Constructor Executed\n" ;
a = a1;
b = b1;
}
void display()
{
cout << a << "\t" << b;
cout << endl;
}
~GFG() { cout << "Destructor Executed\n" ; }
};
int main()
{
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++
#include <iostream>
using namespace std;
class Complex {
int real, img;
public :
Complex() {}
Complex( int r, int i)
{
real = r;
img = i;
}
int getReal() { return real; }
int getImg() { return img; }
Complex operator+(Complex c)
{
Complex temp;
temp.real = this ->real + c.real;
temp.img = this ->img + c.img;
return temp;
}
};
int main()
{
Complex c3;
c3 = Complex({ 2, 3 }) + Complex({ 5, 6 });
cout << c3.getReal() << "+" << c3.getImg() << "i"
<< endl;
return 0;
}
|
Share your thoughts in the comments
Please Login to comment...