Frage zu typedef



  • hi, ich habe ne frage zu typedef:

    ist :

    typedef int blabla;
    

    nicht das gleiche wie :

    #define blabla int
    

    ?



  • wtopic-and-f-is-15.html schrieb:

    hi, ich habe ne frage zu typedef:

    ist :

    typedef int blabla;
    

    nicht das gleiche wie :

    #define blabla int
    

    ?

    Nein. '#define' ist eine reine Textersetzung. 'typedef' hingegen beachtet Sichtbarkeitsbereiche, Vererbung und einiges mehr. Kurz: 'typedef' ist ein Feature des Typensystems von C++, Typenaliase anzulegen. '#define' hingegen arbeitet auf Textebene und kennt keine C++-Semantik.



  • wtopic-and-f-is-15.html schrieb:

    hi, ich habe ne frage zu typedef:

    ist :

    typedef int blabla;
    

    nicht das gleiche wie :

    #define blabla int
    

    ?

    Probier's aus:

    #include <string>
    
    namespace A {
       typedef int myType;
    }
    
    namespace B {
       typedef std::string myType;
    }
    
    using namespace A;
    
    int main() {
       myType x = 1;
       return x;
    }
    

    mit

    #include <string>
    
    namespace A {
       #define myType int
    }
    
    namespace B {
       #define myType std::string
    }
    
    using namespace A;
    // oder, für einen anderen Fehler: using A::myType;
    
    int main() {
       myType x = 1;
       return x;
    }
    

    Gruß,

    Simon2.


Anmelden zum Antworten