473,383 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

struct, typedef, and template

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<floatvector;

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

Thanks!
Ed.


Aug 25 '08 #1
4 2888
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.
Aug 25 '08 #2
Ed
On Aug 25, 4:45*pm, Ian Collins <ian-n...@hotmail.comwrote:
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?
Aug 25 '08 #3
On Mon, 25 Aug 2008 11:00:37 +0200, Ed <se*****@gmail.comwrote:
On Aug 25, 4:45Â*pm, Ian Collins <ian-n...@hotmail.comwrote:
>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.

Aug 25 '08 #4
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<floatvector;

You know that we can not add a typedef to a template struct. So, I am
wondering if this is OK.
Yes.
Aug 25 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Feldhaus | last post by:
Hello, I am working on the migration of our MFC Application written in VS C++ 6.0 to VS .Net 2003. I am getting an error I cannot solve, cause I am not firm with templates and class definitions....
4
by: pradosh | last post by:
Is it possible to typedef a template class? How? For example: template <class T> class OldClass { }; typedef OldClass<T> NewClass<T>;
8
by: Guillaume Dargaud | last post by:
I just saw the following two lines in /usr/include/usb.h and my head is spinning: struct usb_dev_handle; typedef struct usb_dev_handle usb_dev_handle; What is that supposed to mean ? I...
17
by: Ron Peterson (012ED25E) | last post by:
I ran into something like the following while looking into how the GNU MP library implements its mpz_t type. typedef struct { int len; char *buf; } foo; This is an interesting technique. ...
0
by: PengYu.UT | last post by:
Hi, I'm wondering if it is possible to define typedef template? For example, I have template class A. I want give it an alias such as B. The problem comes from refactoring the code. Suppost A...
0
by: colin | last post by:
Hi, I have many suitable structures wich I need to handle in a binary fashion, I can take the address of a struct inside unsafe and fixed block and this works when the struct is pased as a ref...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.