struct, typedef, and template

E

Ed

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.
 
I

Ian Collins

Ed said:
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.
 
R

Rolf Magnus

Ed said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top