Skip to content

Instantly share code, notes, and snippets.

@rahulbhadani
Created May 23, 2022 02:03
Show Gist options
  • Save rahulbhadani/d39d2f17a204faaab836f638303a1498 to your computer and use it in GitHub Desktop.
Save rahulbhadani/d39d2f17a204faaab836f638303a1498 to your computer and use it in GitHub Desktop.
Named Return Value Optimization
#include <iostream>
using namespace std;
class OBJECT
{
public:
const char *a;
OBJECT()
{
cout<<"Constructor"<<endl;
}
OBJECT(const char *ptr)
{
cout<<"Constructor"<<endl;
}
OBJECT(OBJECT &obj)
{
cout<<"copy constructor"<<endl;
}
OBJECT(OBJECT&& obj)
{
cout<<"Move constructor"<<endl;
}
~OBJECT()
{
cout<<"Destructor"<<endl;
}
};
OBJECT func1()
{
OBJECT obj; return obj;
}
int main()
{
OBJECT object;
OBJECT obj1(func1()); //NRVO
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment