-
x = foo();
Alright first lets look at a sample piece of code before I ask my question.
Code:
class foo
{
public:
foo() { x = 0; }
void put( int i ) { x = i; }
int get( void ) { return x; }
private:
int x;
};
int main()
{
foo bar;
bar.put( 1 );
cout << bar.get() << endl;
bar = foo();
cout << bar.get() << endl;
}
Ok my question is about the line "bar = foo();" Now I know what happens to bar, when i call bar.get() I output a 0. But what I'm wondering is, what exactly is that line of code doing there? Is it making bar call it's constructor again, or is it creating a temporary object of foo and copying that to bar then freeing itself, or not freeing itself staying in memory wasting space? Or is it doing something not in that list?
Thanks.
-
it does 3 things. Firstly a temporary foo object is made with the default constructor. Then the assignment operator is called to assign that temp to bar.Then finally the temp is destroyed by calling the destructor.
-
Ah ok that's what I figured what was going on but untill I was absolute I just had to ask. :) Thanks.