Konstruktor bei temporaerem Objekt



  • Hi,

    ich habe mir gerade einen kleinen Test geschrieben und bin etwas verwundert, warum das temporaere Objekt bei return _a zwar seinen Destruktor abarbeitet, aber nie einen Konstruktor. Kann mir das einer bitte erklaeren?

    #include <iostream>
    
    using namespace std;
    
    class A
    {
    private:
        int foo;
    public:
        A() { cout << "A(" << this << "):ctor" << endl; }
        ~A() { cout << "A(" << this << "):dtor" << endl; }
    };
    
    class B
    {
    private:
        int foo;
        A _a;
    public:
        B() {  cout << "B(" << this << "):ctor" << endl; }
        ~B() { cout << "B(" << this << "):dtor" << endl; }
        A get() { cout << "B(" << this << "):get()" << endl; return _a; }
    
    };
    
    int main()
    {
        {
            cout << "B b;" << endl;
            B b;
            cout << "A a2;" << endl;
            A a2;
            cout << "a2 = b.get();" << endl;
            a2 = b.get();
            cout << "leave scope" << endl;
        }
    
        return 0;
    }
    

    Ausgabe:

    B b;
    A(0x7fff5b8fcb34):ctor
    B(0x7fff5b8fcb30):ctor
    A a2;
    A(0x7fff5b8fcb20):ctor
    a2 = b.get();
    B(0x7fff5b8fcb30):get()
    A(0x7fff5b8fcb18):dtor  <-- wo ist dazu der ctor?
    leave scope
    A(0x7fff5b8fcb20):dtor
    B(0x7fff5b8fcb30):dtor
    A(0x7fff5b8fcb34):dtor
    


  • Du loggst ja gar nicht den Kopierkonstruktor (geschweige denn Move-)!



  • (Nicht, dass du den Move-Ctor definieren musst, mit dem Copy-Ctor siehst du schon alles)



  • Stimmt, ich dachte mir halt, wo ein dtor ist muss auch ein ctor sein.



  • So dank der Ausgabe des Kopiekonstruktors macht es jetzt auch Sinn, danke Arcoth_

    #include <iostream>
    
    using namespace std;
    
    class A
    {
    private:
        int foo;
    public:
        A() { cout << "A(" << this << "):ctor" << endl; }
        A(const A& a) {  cout << "A(" << this << "):copy ctor" << endl; }
        ~A() { cout << "A(" << this << "):dtor" << endl; }
    };
    
    class B
    {
    private:
        int foo;
        A _a;
    public:
        B() {  cout << "B(" << this << "):ctor" << endl; }
        B(const B& b) {  cout << "B(" << this << "):copy ctor" << endl; }
        ~B() { cout << "B(" << this << "):dtor" << endl; }
        A get() { cout << "B(" << this << "):get()" << endl; return _a; }
    
    };
    
    int main()
    {
        {
            cout << "B b;" << endl;
            B b;
            cout << "A a2;" << endl;
            A a2;
            cout << "a2 = b.get();" << endl;
            a2 = b.get();
            cout << "leave scope" << endl;
        }
    
        return 0;
    }
    
    B b;
    A(0x7fff5478eb34):ctor
    B(0x7fff5478eb30):ctor
    A a2;
    A(0x7fff5478eb20):ctor
    a2 = b.get();
    B(0x7fff5478eb30):get()
    A(0x7fff5478eb18):copy ctor <-- Da ist die Loesung
    A(0x7fff5478eb18):dtor
    leave scope
    A(0x7fff5478eb20):dtor
    B(0x7fff5478eb30):dtor
    A(0x7fff5478eb34):dtor
    

Anmelden zum Antworten