Skip to content

Instantly share code, notes, and snippets.

@sergeant-wizard
Created September 19, 2014 14:46
Show Gist options
  • Save sergeant-wizard/8cfeffe9c92359cba602 to your computer and use it in GitHub Desktop.
Save sergeant-wizard/8cfeffe9c92359cba602 to your computer and use it in GitHub Desktop.
return value optimization
#include <iostream>
#include <unistd.h>
class myClass{
public:
myClass(){
std::cout << "constructor called" << std::endl;
}
~myClass(){
std::cout << "destructor called" << std::endl;
}
myClass(const myClass& other){
std::cout << "const copy constructor called" << std::endl;
}
myClass(myClass& other){
std::cout << "copy constructor called" << std::endl;
}
};
myClass func(){
return myClass();
}
int main(){
myClass b(func());
myClass c;
myClass d(b);
c = func();
return 0;
}
/*
constructor called
constructor called
copy constructor called
constructor called
destructor called
destructor called
destructor called
destructor called
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment