typedef - c++

This is a discussion on typedef - c++ ; Hi, just wondering: are there any relevant differences between struct S { /* ... */ }; and typedef struct { /* ... */ } S; ? TIA, Tjark -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time ...

+ Reply to Thread
Results 1 to 5 of 5

typedef

  1. Default typedef

    Hi,

    just wondering: are there any relevant differences between

    struct S { /* ... */ };

    and

    typedef struct { /* ... */ } S;

    ?

    TIA,
    Tjark


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  2. Default Re: typedef

    In article <1185271469.646051.298710@d55g2000hsg.googlegroups.com>,
    tjark.weber says...
    > Hi,
    >
    > just wondering: are there any relevant differences between
    >
    > struct S { /* ... */ };
    >
    > and
    >
    > typedef struct { /* ... */ } S;


    Using the first form, the class name is defined (but incomplete) inside
    of the class definition, so you can use it anywhere an incomplete type
    is allowed. For an obvious example, creating a pointer:

    struct S {
    S *s;
    };

    works, but with the typedef:

    typedef struct {
    S *s;
    } S;

    it doesn't -- in this case, 'S' isn't a defined symbol inside of the
    class definition.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  3. Default Re: typedef

    Tjark Weber wrote:
    > Hi,
    >
    > just wondering: are there any relevant differences between
    >
    > struct S { /* ... */ };
    >
    > and
    >
    > typedef struct { /* ... */ } S;
    >
    > ?


    In addition to Jerry Coffin's answer;

    In the former case, you can use the name S as in:

    struct S s;

    but you can't in the latter case, and you can only use it as in:

    S s;

    --
    Seungbeom Kim

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  4. Default Re: typedef

    On Jul 25, 4:24 am, Tjark Weber <tjark.we...> wrote:
    > just wondering: are there any relevant differences between
    > struct S { /* ... */ };
    > and
    > typedef struct { /* ... */ } S;


    Hi Tjark,

    Similar to Jerry and Seungbeom's observations, as your typedef method
    creates an alias to an anonymous struct, you'll find:
    1) GNU preprocessor __PRETTY_FUNCTION__ contains "<anonymous struct>"
    2) some debuggers may lack an information structure name (though some
    tools (e.g. g++/gdb) coordinate use of the typedef name)
    3) you can't define constructors and destructors (because their name
    must match the structure's)
    4) when struct is used, the identifier 'S' exists in a struct-specific
    naming space, such that you could potentially define some other thing
    called 'S', such as a function, without getting a compilation error.
    For example: struct S; void S(int);


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  5. Default Re: typedef

    On Jul 24, 8:24 pm, Tjark Weber <tjark.we...> wrote:
    > Hi,
    >
    > just wondering: are there any relevant differences between
    >
    > struct S { /* ... */ };
    >
    > and
    >
    > typedef struct { /* ... */ } S;
    >
    > ?
    >

    Another couple of differences:
    - You can't define a constructor in the latter form.
    - The latter is not idiomatic C++ (I would reject it in code review
    unless it was code being shared between C and C++).


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


+ Reply to Thread

Similar Threads

  1. Still no typedef
    By Application Development in forum Java
    Replies: 62
    Last Post: 12-25-2007, 05:19 PM
  2. typedef
    By Application Development in forum CSharp
    Replies: 1
    Last Post: 12-12-2007, 10:56 AM
  3. constuctor typedef
    By Application Development in forum c++
    Replies: 0
    Last Post: 02-23-2007, 04:51 PM
  4. What does this typedef mean?
    By Application Development in forum C
    Replies: 2
    Last Post: 08-21-2005, 12:48 AM
  5. typedef.....
    By Application Development in forum C
    Replies: 3
    Last Post: 05-28-2005, 04:17 AM