Thread: Question about compiler copying objects

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008

    Question about compiler copying objects

    This class is just something I've been using to experiment with a couple of things so excuse the mess:

    class BirdHouse
        Bird bo;
        Bird *bp;
        Bird& br;
        int bi;
        BirdHouse operator=(const BirdHouse&);
        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 +;}
        friend ostream& operator<<(ostream& os, const BirdHouse& bh) {
            os << << *bh.bp << << "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:

    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:

    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!

  2. #2
    The larch
    Join Date
    May 2006
    You can probably put cout statements all over to see where the copies happen.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    The first copy is of the a variable to the o parameter. The second is from the o parameter to the bo member variable.

    During adding, the third copy is from the bo member variable of the left-hand-side object to the o parameter. The fourth is from the o parameter to the bo member variable of the temporary created by the addition operator. The fifth happens when the temporary in that function is copied to the return value temporary.

    If you turn optimizations on, you should see that fifth copy disappear.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Thanks, it all seems so obvious when you explained it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 12:08 PM
  2. Objects contractor & compiler options
    By dude543 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2005, 07:05 AM
  3. C/C++ Compiler Question
    By Unregistered in forum C++ Programming
    Replies: 11
    Last Post: 07-09-2002, 02:09 AM
  4. Question: Which Compiler????
    By MaBo in forum C Programming
    Replies: 5
    Last Post: 06-04-2002, 11:57 AM
  5. Compiler question
    By Fool in forum A Brief History of
    Replies: 16
    Last Post: 08-31-2001, 10:36 PM
Website Security Test