struct, typedef, and template

Discussion in 'C++' started by Ed, Aug 25, 2008.

  1. Ed

    Ed Guest

    Hi, guys,
    In C language manner, we need to put a "struct" token before one
    struct variable declaration like following.

    <code>
    struct Apple
    {
    float Price;
    };

    struct Apple apple;
    </code>

    Sometime, we would use typedef to simplify the usage.

    <code>
    typedef struct _Apple
    {
    float Price;
    } Apple;

    Apple apple;
    </code>


    Now, when we use this manner in C++, it seems we don't need to add a
    typedef for the struct declaration.

    <code>
    struct Apple
    {
    float Price;
    };

    Apple apple;
    </code>

    Directly using "Apple" to do the declaration is OK in VS compiler and
    Intel Compiler.
    My question is should I still keep the usage of old struct usage.

    Why I ask this question is because of template.
    I write a template struct.


    template <typename Real>
    struct Vector
    {
    Real x;
    Real y;
    Real z;
    };

    Vector<float> vector;

    You know that we can not add a typedef to a template struct. So, I am
    wondering if this is OK.

    Thanks!
    Ed.
    Ed, Aug 25, 2008
    #1
    1. Advertising

  2. Ed

    Ian Collins Guest

    Ed wrote:
    > Hi, guys,
    > In C language manner, we need to put a "struct" token before one
    > struct variable declaration like following.
    >
    > <code>
    > struct Apple
    > {
    > float Price;
    > };
    >
    > struct Apple apple;
    > </code>
    >
    > Now, when we use this manner in C++, it seems we don't need to add a
    > typedef for the struct declaration.
    >

    Correct. In C++, struts and classes are types.

    --
    Ian Collins.
    Ian Collins, Aug 25, 2008
    #2
    1. Advertising

  3. Ed

    Ed Guest

    On Aug 25, 4:45 pm, Ian Collins <> wrote:
    > Ed wrote:
    > > Hi, guys,
    > > In C language manner, we need to put a "struct" token before one
    > > struct variable declaration like following.

    >
    > > <code>
    > > struct Apple
    > > {
    > >    float Price;
    > > };

    >
    > > struct Apple apple;
    > > </code>

    >
    > > Now, when we use this manner in C++, it seems we don't need to add a
    > > typedef for the struct declaration.

    >
    > Correct.  In C++, struts and classes are types.
    >
    > --
    > Ian Collins.


    Is this the rule in C++ standard?
    Ed, Aug 25, 2008
    #3
  4. On Mon, 25 Aug 2008 11:00:37 +0200, Ed <> wrote:

    > On Aug 25, 4:45 pm, Ian Collins <> wrote:
    >> Ed wrote:
    >> > Hi, guys,
    >> > In C language manner, we need to put a "struct" token before one
    >> > struct variable declaration like following.

    >>
    >> > <code>
    >> > struct Apple
    >> > {
    >> >    float Price;
    >> > };

    >>
    >> > struct Apple apple;
    >> > </code>

    >>
    >> > Now, when we use this manner in C++, it seems we don't need to add a
    >> > typedef for the struct declaration.

    >>
    >> Correct.  In C++, struts and classes are types.
    >>
    >> --
    >> Ian Collins.

    >
    > Is this the rule in C++ standard?
    >

    Yep.
    David Côme, Aug 25, 2008
    #4
  5. Ed

    Rolf Magnus Guest

    Ed wrote:

    > Directly using "Apple" to do the declaration is OK in VS compiler and
    > Intel Compiler.


    It's standard C++.

    > My question is should I still keep the usage of old struct usage.


    Only for those parts of your code that might need to be used from C code.

    > Why I ask this question is because of template.
    > I write a template struct.
    >
    >
    > template <typename Real>
    > struct Vector
    > {
    > Real x;
    > Real y;
    > Real z;
    > };
    >
    > Vector<float> vector;
    >
    > You know that we can not add a typedef to a template struct. So, I am
    > wondering if this is OK.


    Yes.
    Rolf Magnus, Aug 25, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DanielEKFA
    Replies:
    8
    Views:
    606
    DanielEKFA
    May 16, 2005
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,373
    Chris Fogelklou
    Apr 20, 2004
  3. Steven T. Hatton
    Replies:
    2
    Views:
    678
  4. Adam Nielsen
    Replies:
    3
    Views:
    407
  5. oor
    Replies:
    0
    Views:
    1,346
Loading...

Share This Page