struct, typedef, and template

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ed

    struct, typedef, and template

    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<floatvec tor;

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

    Thanks!
    Ed.






  • Ian Collins

    #2
    Re: struct, typedef, and template

    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.

    Comment

    • Ed

      #3
      Re: struct, typedef, and template

      On Aug 25, 4:45 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      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?


      Comment

      • =?utf-8?Q?David_C=C3=B4me?=

        #4
        Re: struct, typedef, and template

        On Mon, 25 Aug 2008 11:00:37 +0200, Ed <seahalo@gmail. comwrote:
        On Aug 25, 4:45 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        >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.

        Comment

        • Rolf Magnus

          #5
          Re: struct, typedef, and template

          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<floatvec tor;
          >
          You know that we can not add a typedef to a template struct. So, I am
          wondering if this is OK.
          Yes.


          Comment

          Working...