operator==?



  • Hallihallo

    class A
    {
    private:
    	int i;
    };
    
    class B:protected A
    {
    	friend bool operator==(const A& a,const B& b)
    	{
    		return a==static_cast<const A&>(b);
    	}
    };
    
    int main()
    {
    	A a;
    	a==a;
    	B b;
    	a==b;
    }
    
    1>d:\projekte\test2\main.cpp(11) : error C2679: binary '==' : no operator found which takes a right-hand operand of type 'const A' (or there is no acceptable conversion)
    1>        d:\projekte\test2\main.cpp(9): could be 'bool operator ==(const A &,const B &)'
    1>        while trying to match the argument list '(const A, const A)'
    1>d:\projekte\test2\main.cpp(18) : error C3767: '==': candidate function(s) not accessible
    1>        could be the friend function at 'd:\projekte\test2\main.cpp(9)' : '=='  [may be found via argument-dependent lookup]
    1>d:\projekte\test2\main.cpp(18) : error C2676: binary '==' : 'A' does not define this operator or a conversion to a type acceptable to the predefined operator
    

    Fragen:
    1. Wieso wird nicht implit ein operator==(const A&,const A&) erzeugt?
    2. ist die Klasse B so korrekt?

    Danke...



  • Tatütata schrieb:

    1. Wieso wird nicht implit ein operator==(const A&,const A&) erzeugt?

    Wie soll das funktionieren? Soll der Kompiler raten welcher Member dir wichtiger ist? 😃

    Tatütata schrieb:

    2. ist die Klasse B so korrekt?

    Nein,
    sollte ungefähr so aussehen

    class A
    {
    public:
        bool operator == (const A& other) const
        {
            return this->i == other.i;
        }
    
    private:
        int i;
    };
    
    class B : protected A
    {
    public:
        bool operator == (const A& other) const
        {
            return dynamic_cast <const A&> (*this) == other;
        }
    };
    
    bool operator == (const A& a, const B& b)
    {
        return b == a;
    }
    
    int main()
    {
        A a;
    	a==a;
        B b;
    	a==b;
    
    	return 0;
    }
    

    Gruß
    Don06



  • Don06 schrieb:

    Tatütata schrieb:

    1. Wieso wird nicht implit ein operator==(const A&,const A&) erzeugt?

    Wie soll das funktionieren? Soll der Kompiler raten welcher Member dir wichtiger ist? 😃

    Ich dachte, der Compiler erstellt implizit einen operator== für jede Klasse. Naja, wenn es nicht so ist, dann ist ja alles ok. danke.



  • Don06 schrieb:

    Tatütata schrieb:

    2. ist die Klasse B so korrekt?

    Nein,

    Doch, stimmt so. Deine Version geht auch. Der 'dynamic_cast' ist aber in jedem Fall unnötig und irreführend: Das 'B' ist *immer* auch ein 'A', von daher reicht ein 'static_cast'.



  • Tatütata schrieb:

    Don06 schrieb:

    Tatütata schrieb:

    1. Wieso wird nicht implit ein operator==(const A&,const A&) erzeugt?

    Wie soll das funktionieren? Soll der Kompiler raten welcher Member dir wichtiger ist? 😃

    Ich dachte, der Compiler erstellt implizit einen operator== für jede Klasse. Naja, wenn es nicht so ist, dann ist ja alles ok. danke.

    Nein, macht er nicht - er erstellt einen Zuweisungoperator (der kopiert alle Datenelemente), aber keinen Vergleichsoperator).


Anmelden zum Antworten