[c++] Typedef - Warum zwei Namen?

Es gibt 10 Antworten in diesem Thema. Der letzte Beitrag () ist von LucaWelker.

    [c++] Typedef - Warum zwei Namen?

    Hey Commuity,

    ich habe eine Frage, warum benötigt man in C++ bei Typedef zwei namen, einmal den oben und einmal den unten:

    Beispiel:

    Quellcode

    1. typedef struct _Name1 //???
    2. {
    3. string FullName;
    4. double HourlySalary;
    5. }Name2; //???


    Ich hoffe es kann mir jemand helfen:)

    lg.

    LucaWelker
    lg.

    LucaWelker
    Benötigt man nicht. Ist Äquivalent zu:

    Quellcode

    1. struct Name2
    2. {
    3. string FullName;
    4. double HourlySalary;
    5. };
    Du guckst wahrscheinlich in den alten WinAPI-Headern nach. Damals in C war es Gang- und Gebe, es so zu schreiben. Grund war z.B., dass man es meist so machte:

    Quellcode

    1. struct _TEST { ... } TEST, *LPTEST;


    Gruß
    To make foobar2000 a real random music player, I figured out the only way to achieve this is to use Windows Media Player.

    At some point in time, you recognize that knowing more does not necessarily make you more happy.
    Also reicht es wenn ich den Namen oben habe.. Nachdem ich eine Weile gegooglet habe hab ich noch bei Googlebooks was gefunden.. Da sieht es aus als wäre es der "Datentyp"?!

    lg.

    LucaWelker

    Quelle: books.google.de/books?id=xbZkw…wBzgU#v=onepage&q&f=false
    lg.

    LucaWelker
    'typedef'en ist nichts anderes als einem bestimmten Typen einen anderen Namen zu geben. Wenn du Spaß daran hast, kannst du auch folgendes machen:

    Quellcode

    1. typedef int Zahl;
    2. typedef float Fliesskommazahl;
    3. typedef std::string Text;
    Nötig ist das ganze aber gewiss nicht. Das wird erst Interessant, wenn es um längere Datentypen geht, á la

    Quellcode

    1. typedef std::map<std::string, std::vector<std::string>> strmap_t;


    Gruß
    To make foobar2000 a real random music player, I figured out the only way to achieve this is to use Windows Media Player.

    At some point in time, you recognize that knowing more does not necessarily make you more happy.
    Wenn du einen "struct" definierst ist er bereits ein vollwertiger Typ. Ein typedef ist dazu nicht nötig.

    Gruß
    To make foobar2000 a real random music player, I figured out the only way to achieve this is to use Windows Media Player.

    At some point in time, you recognize that knowing more does not necessarily make you more happy.
    Okey.. ich dachte weil dass in dem Googlebuch steht :D

    Sie können die Typendeklaration auch direkt mit der Structurdefinition verbinden...


    oder versteh ich da grade einfach was falsch ;)

    lg.

    LucaWelker
    lg.

    LucaWelker
    deklarieren != definieren.

    deklarieren: (z.B. für forward-declarations)

    Quellcode

    1. struct Name;


    definieren:

    Quellcode

    1. struct Name
    2. {
    3. // ... Member ...
    4. };


    Gruß
    To make foobar2000 a real random music player, I figured out the only way to achieve this is to use Windows Media Player.

    At some point in time, you recognize that knowing more does not necessarily make you more happy.
    Dann ist das Buch wohl an der Stelle nicht so ganz genau. Man könnte es so beschreiben: Die Struktur wird definiert und gleichzeitig wird eine Typendeklaration durchgeführt. Stell dir 'typedef's einfach als Nicknamen für bekannte Daten vor ;)

    Gruß
    To make foobar2000 a real random music player, I figured out the only way to achieve this is to use Windows Media Player.

    At some point in time, you recognize that knowing more does not necessarily make you more happy.