konstruktor der nicht da ist?



  • Hallo

    gcc 4.4 akzeptiert folgenden Code ohne Probleme:

    class foo
    {
    };
    
    struct bar
    {
    };
    
    int main()
    {
        bar b(foo());
    }
    

    Ist dies legales C++? Falls ja, wo steht dies im Standart?



  • If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A(). This constructor is an inline public member of its class. The compiler will implicitly define A::A() when the compiler uses this constructor to create an object of type A. The constructor will have no constructor initializer and a null body.



  • EinHeld111 schrieb:

    class foo
    {
    };
    
    struct bar
    {
    };
    
    int main()
    {
        bar b(foo());
    }
    

    Damit deklarierst du bloss eine lokale Funktion.

    Davon abgesehen, dass dir C++ einen Default-Ctor schenkt, wenn du selbst keinen deklarierst, was ja schon erwähnt wurde.



  • hustbaer schrieb:

    EinHeld111 schrieb:

    class foo
    {
    };
    
    struct bar
    {
    };
    
    int main()
    {
        bar b(foo());
    }
    

    Damit deklarierst du bloss eine lokale Funktion.

    Davon abgesehen, dass dir C++ einen Default-Ctor schenkt, wenn du selbst keinen deklarierst, was ja schon erwähnt wurde.

    Interessant

    class foo
    {
    };
    
    struct bar
    {
    	int i;
    };
    
    int main()
    {
        bar b(foo());
    	b.i=0;
    }
    

    Wenn bar b(foo()); aber eine Funktionsdeklaration ist, wieso funktioniert dann dies?



  • Unter welchem Compiler funktioniert das denn? - VC08 einmal sicher nicht..



  • drakon schrieb:

    Unter welchem Compiler funktioniert das denn? - VC08 einmal sicher nicht..

    Das heißt nichts.
    Ich nutze gcc 4.4 ...

    #include <utility>
    
    class foo
    {
    };
    
    struct bar
    {
    };
    
    int main()
    {
        bar b(foo()); //OK
    
    	foo f;
        bar b2(f); //Fehler
    
    	bar b3(std::move(f)); //Fehler
    
    	static_cast<bar>(f); //Fehler
    
    	static_cast<bar>(foo()); //Fehler
    }
    


  • EinHeld111 schrieb:

    hustbaer schrieb:

    EinHeld111 schrieb:

    class foo
    {
    };
    
    struct bar
    {
    };
    
    int main()
    {
        bar b(foo());
    }
    

    Damit deklarierst du bloss eine lokale Funktion.

    Davon abgesehen, dass dir C++ einen Default-Ctor schenkt, wenn du selbst keinen deklarierst, was ja schon erwähnt wurde.

    Interessant

    class foo
    {
    };
    
    struct bar
    {
    	int i;
    };
    
    int main()
    {
        bar b(foo());
    	b.i=0;
    }
    

    Wenn bar b(foo()); aber eine Funktionsdeklaration ist, wieso funktioniert dann dies?

    EinHeld111 schrieb:

    drakon schrieb:

    Unter welchem Compiler funktioniert das denn? - VC08 einmal sicher nicht..

    Das heißt nichts.
    Ich nutze gcc 4.4 ...

    #include <utility>
    
    class foo
    {
    };
    
    struct bar
    {
    };
    
    int main()
    {
        bar b(foo()); //OK
    
    	foo f;
        bar b2(f); //Fehler
    
    	bar b3(std::move(f)); //Fehler
    
    	static_cast<bar>(f); //Fehler
    
    	static_cast<bar>(foo()); //Fehler
    }
    

    Ahh, sorry, ich habe mist erzählt. Jetzt ist alles klar 😃

    Danke!


Anmelden zum Antworten