C++0x decltype und Vererbung



  • Hallo

    Darf man in C++0x folgendes schreiben?

    struct foo
    {
      typedef decltype(*this) ThisType;
      void operator= (const ThisType& OtherFoo)
      {
        std::memcpy(this, &OtherFoo, sizeof ThisType);
      };
    };
    struct bar : foo
    {  
    };
    

    Dannach hat bar einen Zuweisungsoperator.

    MfG EOutOfResources



  • Nein.



  • audacia schrieb:

    Nein.

    Grund?



  • decltype - In the C++ programming language, decltype is an operator for querying the type of an expression.



  • EOutOfResources schrieb:

    Darf man in C++0x folgendes schreiben?

    struct foo
    {
      typedef decltype(*this) ThisType;
      void operator= (const ThisType& OtherFoo)
      {
        std::memcpy(this, &OtherFoo, sizeof ThisType);
      };
    };
    struct bar : foo
    {  
    };
    

    Dannach hat bar einen Zuweisungsoperator.

    Ich denke nicht, dass die Verwendung von this in diesem Kontext erlaubt ist. Ich glaube, this darf nur innerhalb der Definition einer nicht-statischen Elementfunktion auftauchen.

    Das, was Du mit Deinem Code erreichen willst, ist mir nicht klar. Der Zuweisungsoperator ist sowieso etwas besonderes. Der Compiler deklariert das Ding ganz automatisch in bar, unabhängig davon, was ThisType bei Dir ist. Diese vom Compiler erzeugte Deklaration von bar::operator=(bar const); "versteckt" den bzw die Operatore(n) foo::operator= aus der Basisklasse.

    Übrigens wäre decltype(*this) bei

    struct foo {
      void blah1() {}
      void blah2() const {}
    };
    

    innerhalb von blah1 der Typ foo& und innerhalb von blah2 der Typ foo const&

    Eventuell ist CRTP noch etwas für Dich (curiously recurring template pattern).

    kk


Anmelden zum Antworten