Kopiowanie konstruktorów i operatorów przydzielania

Napisałem następujący program, aby sprawdzić, kiedy wywoływany jest konstruktor kopiowania i kiedy operator przypisania jest nazywany:


#include 

class Test
{
public:
    Test() :
        iItem (0)
    {
        std::cout << "This is the default ctor" << std::endl;
    }

    Test (const Test& t) :
        iItem (t.iItem)

    {
        std::cout << "This is the copy ctor" << std::endl;
    }

    ~Test()
    {
        std::cout << "This is the dtor" << std::endl;
    }

    const Test& operator=(const Test& t)
    {
        iItem = t.iItem;    
        std::cout << "This is the assignment operator" << std::endl;
        return *this;
    }

private:
    int iItem;
};

int main()
{
    {
        Test t1;
        Test t2 = t1;
    }
    {
        Test t1;
        Test t2 (t1);
    }
    {
        Test t1;
        Test t2;
        t2 = t1;
    }
}

Powoduje to następujące wyjście (po prostu dodano linie empy, aby było to bardziej zrozumiałe):

doronw@DW01:~$ ./test
This is the default ctor
This is the copy ctor
This is the dtor
This is the dtor

This is the default ctor
This is the copy ctor
This is the dtor
This is the dtor

This is the default ctor
This is the default ctor
This is the assignment operator
This is the dtor
This is the dtor


Drugi i trzeci zestaw zachowują się zgodnie z oczekiwaniami, ale w pierwszym zestawie wywoływany jest konstruktor kopiowania, nawet jeśli używany jest operator przypisania.

Czy to zachowanie jest częścią standardu C ++, czy tylko sprytną optymalizacją kompilatora (używam gcc 4.4.1)

questionAnswers(3)

yourAnswerToTheQuestion