I cannot figure out why in the next piece of code, the defined copy constuctor doesn't print...
#include <iostream>
using namespace std;
class B {
static int count;
int data;
int id;
void print(const char* p)
{
cout <<p <<", "<<id <<", " << data << endl;
}
public:
B(int d=0)
{
data=d; id=++count; print("B(int)");
}
B( const B& a)
{
data=a.data; id=++count; print("B(cost B&)");
}
~B(){print("~B()");}
operator bool(){ return (bool)data;}
B operator+(int i){print("operator+"); return B(data+i);}
};
int B::count=0;
void main(){
B b(42);
B x=b+2;
bool z=b+1;
getchar();
}
I expected to get a copy constructor print with B x=b+2
but it doesn't show.
Any ideas?
Thanks,
The output:
B(int), 1, 42
operator+, 1, 42
B(int), 2, 44
operator+, 1, 42
B(int), 3, 43
~B(), 3, 43
So it's return value optimization?