This class is just something I've been using to experiment with a couple of things so excuse the mess:
Code:
class BirdHouse
{
private:
Bird bo;
Bird *bp;
Bird& br;
int bi;
BirdHouse operator=(const BirdHouse&);
public:
BirdHouse(Bird o, Bird *p, Bird& r, int bibi): bo(o), bp(p), br(r), bi(bibi){}
BirdHouse operator+(const BirdHouse& rhs) {return BirdHouse(bo, (new Bird(*rhs.bp)), br, (bi + rhs.bi));}
friend ostream& operator<<(ostream& os, const BirdHouse& bh) {
os << bh.bo << *bh.bp << bh.br << "bi: " << bh.bi << endl;
return os;
}
};
The Bird class has a copy constructor which adds "copy" onto a string identifier so I can see how many copies of the object has been made. When I construct a BirdHouse with some Birds like so:
Code:
Bird a, b, c, d;
BirdHouse bh1(a, &b, c, 5);
And then I cout<< it, I can see that the Bird object 'a' has been copied twice. Am I right in assuming that these copies consist of the actual parameter being copied into the formal parameter of the BirdHouse constructor - and then that parameter being copied again for the initialization list? That's all I can think of.
If I then go ahead and add the BirdHouse to itself using the operator+ and cout<< the result like this:
Code:
cout << (bh1 + bh1);
It shows that the 'a' object has been copied a total of 5 times, which means that calling the operator+ has copied it 3 more times. But I'm at a loss to see where these 3 copies occur. I don't know if I've articulated this adequately, but any explanation would be greatly appreciated if I have!