Ist das denn nicht Standard?



  • Ich kriege bei folgendem Code Compilerfehler mit VC++ 6, kann mir bitte jemand den Grund nennen???

    class A {};
    class B : public A {};
    class C : public A {};
    
    void f(A*) {}
    
    void test()
    {
    	B* b = new B;
    	C* c = new C;
    	f(b ? b : (A*) c); //Geht durch
    	f(b ? b : c);      //error C2446: ':' : no conversion from 'class C *' to 'class B *'
    }
    


  • Krösus schrieb:

    Ich kriege bei folgendem Code Compilerfehler mit VC++ 6, kann mir bitte jemand den Grund nennen???

    Mal ganz salopp gesagt: Der zweite und dritte Operand des ternären Operators müssen vom selben Typ (oder void) sein, wenn es sich bei den Typen dieser Operanden nicht um Udts handelt.

    Dein Compiler hat also ganz recht.



  • C* ist aber nicht nach B* konvertierbar. 🙂 Du meinst dass C* und B* nach A* konvertierbar sind.


  • Mod

    a ? b : c

    b und c müssen den gleichen typ haben, oder es muss eine umwandlung b -> c oder c -> b möglich sein. das ist in dem statement, das den fehler erzeugt, aber nicht der fall.

    The following rules apply to the second and third expressions:

    If both expressions are of the same type, the result is of that type.
    If both expressions are of arithmetic or enumeration types, the usual arithmetic conversions (covered in Arithmetic Conversions) are performed to convert them to a common type.
    If both expressions are of pointer types or if one is a pointer type and the other is a constant expression that evaluates to 0, pointer conversions are performed to convert them to a common type.
    If both expressions are of reference types, reference conversions are performed to convert them to a common type.
    If both expressions are of type void, the common type is type void.
    If both expressions are of a given class type, the common type is that class type.
    Any combinations of second and third operands not in the preceding list are illegal. The type of the result is the common type, and it is an l-value if both the second and third operands are of the same type and both are l-values.



  • Bashar schrieb:

    C* ist aber nicht nach B* konvertierbar. 🙂 Du meinst dass C* und B* nach A* konvertierbar sind.

    Ups 🙂



  • Camper: Woher ist die Liste? Nicht aus dem Standard jedenfalls, und auch nicht korrekt:

    int x=0,y=1, &r=x;
    x<y ? y : r;
    

    Das ist legal.


  • Mod

    ist aus dem msdn ... mit 'C++ Language Reference' überschrieben... (kein microsoft specific) naja, insofern wäre es gültig für vc.

    ich will mich ja auch gar nicht mit dir streiten. 🙂

    allerdings nützen standard vorgaben ja nicht viel, wenn der compiler, der benutzt wird, sie nicht umsetzt. folglich ist bei vc immer erst msdn dran 😉



  • Dann probier mein Beispiel mal mit dem vc.


  • Mod

    funktioniert - dann saugt eben die dokumentation :p
    ich hab sie nicht geschrieben 🙂



  • @Krösus
    übrigens ist es nicht üblich, dass die new-Operatoren NULL bei einem Fehler zurückgeben. Eigentlich werfen die die Ausnahme std::bad_alloc. Beim VC++6 ist das wohl anders. Aber bei neueren Compilern sollte der Code nicht portabel sein!


Anmelden zum Antworten